<commit_before>/*
 * Copyright (C) 2004 <NAME>
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice,this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.a0z.mpd;

import org.a0z.mpd.exception.MPDConnectionException;
import org.a0z.mpd.exception.MPDNoResponseException;
import org.a0z.mpd.exception.MPDServerException;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

/**
 * Class representing a connection to MPD Server.
 */
abstract class MPDConnection {

    private static final String MPD_RESPONSE_ERR = "ACK";

    private static final String TAG = "MPDConnection";

    private static final String POOL_THREAD_NAME_PREFIX = "pool";

    private static final int CONNECTION_TIMEOUT = 10000;

    private static final int DEFAULT_BUFFER_SIZE = 1024;

    private static final String MPD_RESPONSE_OK = "OK";

    private static final String MPD_CMD_START_BULK = "command_list_begin";

    private static final String MPD_CMD_START_BULK_OK = "command_list_ok_begin";

    private static final String MPD_CMD_BULK_SEP = "list_OK";

    private static final String MPD_CMD_END_BULK = "command_list_end";

    private static final int MAX_CONNECT_RETRY = 3;

    private static final int MAX_REQUEST_RETRY = 3;

    private static final Pattern PERIOD_DELIMITER = Pattern.compile("\\.");

    private final List<MPDCommand> mCommandQueue;

    private final ExecutorService mExecutor;

    private final InetAddress mHostAddress;

    private final int mHostPort;

    private final int mMaxThreads;

    private final int mReadWriteTimeout;

    private boolean mIsAlbumGroupingSupported = false;

    private boolean mCancelled = false;

    private int mCommandQueueStringLength;

    private boolean mIsConnected = false;

    private int[] mMPDVersion = null;

    private String mPassword = null;

    MPDConnection(final InetAddress server, final int port, final int readWriteTimeout,
            final int maxConnections, final String password) {
        super();
        mReadWriteTimeout = readWriteTimeout;
        mHostPort = port;
        mHostAddress = server;
        mCommandQueue = new ArrayList<>();
        mCommandQueueStringLength = MPD_CMD_START_BULK_OK.length() + MPD_CMD_END_BULK.length() + 5;
        mMaxThreads = maxConnections;
        mExecutor = Executors.newFixedThreadPool(mMaxThreads);
        mPassword = password;
    }


    MPDConnection(final InetAddress server, final int port, final String password,
            final int readWriteTimeout) {
        this(server, port, readWriteTimeout, 1, password);
    }

    private static List<String[]> separatedQueueResults(final Iterable<String> lines) {
        final List<String[]> result = new ArrayList<>();
        final ArrayList<String> lineCache = new ArrayList<>();

        for (final String line : lines) {
            if (line.equals(MPD_CMD_BULK_SEP)) { // new part
                if (!lineCache.isEmpty()) {
                    result.add(lineCache.toArray(new String[lineCache.size()]));
                    lineCache.clear();
                }
            } else {
                lineCache.add(line);
            }
        }
        if (!lineCache.isEmpty()) {
            result.add(lineCache.toArray(new String[lineCache.size()]));
        }
        return result;
    }

    final int[] connect() throws MPDServerException {
        int[] result = null;
        int retry = 0;
        MPDServerException lastException = null;

        while (result == null && retry < MAX_CONNECT_RETRY && !mCancelled) {
            try {
                result = innerConnect();
            } catch (final MPDServerException e) {
                lastException = e;
                try {
                    Thread.sleep(500L);
                } catch (final InterruptedException ignored) {
                }
            } catch (final RuntimeException e) {
                lastException = new MPDServerException(e);
            }
            retry++;
        }

        if (result != null) {
            mIsConnected = true;
            mMPDVersion = result;
            return result;
        } else {
            if (lastException == null) {
                lastException = new MPDServerException("Connection request cancelled.");
            }
            throw new MPDServerException(lastException);
        }
    }

    void disconnect() throws MPDConnectionException {
        mCancelled = true;
        innerDisconnect();
    }

    InetAddress getHostAddress() {
        return mHostAddress;
    }
<commit_msg>MPDConnection: connect() return value is discarded, change signature to void.<commit_after>/*
 * Copyright (C) 2004 <NAME>
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice,this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.a0z.mpd;

import org.a0z.mpd.exception.MPDConnectionException;
import org.a0z.mpd.exception.MPDNoResponseException;
import org.a0z.mpd.exception.MPDServerException;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

/**
 * Class representing a connection to MPD Server.
 */
abstract class MPDConnection {

    private static final String MPD_RESPONSE_ERR = "ACK";

    private static final String TAG = "MPDConnection";

    private static final String POOL_THREAD_NAME_PREFIX = "pool";

    private static final int CONNECTION_TIMEOUT = 10000;

    private static final int DEFAULT_BUFFER_SIZE = 1024;

    private static final String MPD_RESPONSE_OK = "OK";

    private static final String MPD_CMD_START_BULK = "command_list_begin";

    private static final String MPD_CMD_START_BULK_OK = "command_list_ok_begin";

    private static final String MPD_CMD_BULK_SEP = "list_OK";

    private static final String MPD_CMD_END_BULK = "command_list_end";

    private static final int MAX_CONNECT_RETRY = 3;

    private static final int MAX_REQUEST_RETRY = 3;

    private static final Pattern PERIOD_DELIMITER = Pattern.compile("\\.");

    private final List<MPDCommand> mCommandQueue;

    private final ExecutorService mExecutor;

    private final InetAddress mHostAddress;

    private final int mHostPort;

    private final int mMaxThreads;

    private final int mReadWriteTimeout;

    private boolean mIsAlbumGroupingSupported = false;

    private boolean mCancelled = false;

    private int mCommandQueueStringLength;

    private boolean mIsConnected = false;

    private int[] mMPDVersion = null;

    private String mPassword = null;

    MPDConnection(final InetAddress server, final int port, final int readWriteTimeout,
            final int maxConnections, final String password) {
        super();
        mReadWriteTimeout = readWriteTimeout;
        mHostPort = port;
        mHostAddress = server;
        mCommandQueue = new ArrayList<>();
        mCommandQueueStringLength = MPD_CMD_START_BULK_OK.length() + MPD_CMD_END_BULK.length() + 5;
        mMaxThreads = maxConnections;
        mExecutor = Executors.newFixedThreadPool(mMaxThreads);
        mPassword = password;
    }


    MPDConnection(final InetAddress server, final int port, final String password,
            final int readWriteTimeout) {
        this(server, port, readWriteTimeout, 1, password);
    }

    private static List<String[]> separatedQueueResults(final Iterable<String> lines) {
        final List<String[]> result = new ArrayList<>();
        final ArrayList<String> lineCache = new ArrayList<>();

        for (final String line : lines) {
            if (line.equals(MPD_CMD_BULK_SEP)) { // new part
                if (!lineCache.isEmpty()) {
                    result.add(lineCache.toArray(new String[lineCache.size()]));
                    lineCache.clear();
                }
            } else {
                lineCache.add(line);
            }
        }
        if (!lineCache.isEmpty()) {
            result.add(lineCache.toArray(new String[lineCache.size()]));
        }
        return result;
    }

    final void connect() throws MPDServerException {
        int[] result = null;
        int retry = 0;
        MPDServerException lastException = null;

        while (result == null && retry < MAX_CONNECT_RETRY && !mCancelled) {
            try {
                result = innerConnect();
            } catch (final MPDServerException e) {
                lastException = e;
                try {
                    Thread.sleep(500L);
                } catch (final InterruptedException ignored) {
                }
            } catch (final RuntimeException e) {
                lastException = new MPDServerException(e);
            }
            retry++;
        }

        if (result == null) {
            if (lastException == null) {
                lastException = new MPDServerException("Connection request cancelled.");
            }
            throw new MPDServerException(lastException);
        }
        mIsConnected = true;
        mMPDVersion = result;
    }

    void disconnect() throws MPDConnectionException {
        mCancelled = true;
        innerDisconnect();
    }

    InetAddress getHostAddress() {
        return mHostAddress;
    }

{'fraction_non_alphanumeric': 0.07641591865914688, 'fraction_numerical': 0.005560409881642704, 'mean_word_length': 2.919676214196762, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4377100', 'n_tokens_mistral': 3515, 'n_tokens_neox': 3263, 'n_words': 1082}