Read data from socket channel into byte array with default timeout - Java java.lang

Java examples for java.lang:byte Array to int

Description

Read data from socket channel into byte array with default timeout

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main{
    private static final int TIMEOUT_TIME_MS = 30000;
    private static final int SLEEP_TIME_MS = 10;
    /**//from  w w w .j  a v  a  2s  . c  om
     * Read data from socket channel into byte array with default timeout
     * 
     * @param sc socket to read data from
     * @param buffer byte array to put data into
     * @return number of bytes read
     * @throws IOException if unable to read data
     */
    public static int readBuffer(SocketChannel sc, byte[] buffer)
            throws IOException {
        return readBuffer(sc, buffer, TIMEOUT_TIME_MS);
    }
    /**
     * Read data from socket channel into byte array with specified timeout
     * 
     * @param sc socket to read data from
     * @param buffer byte array to put data into
     * @param timeoutMS how long to wait for read in MS
     * @return number of bytes read
     * @throws IOException if unable to read data
     */
    public static int readBuffer(SocketChannel sc, byte[] buffer,
            long timeoutMS) throws IOException {

        int offset = 0;
        ByteBuffer bb = ByteBuffer.wrap(buffer);
        bb.clear();
        long sleepMS = 0;
        while (offset < buffer.length) {

            int rc = sc.read(bb);
            if (rc == -1) {
                return -1;
            }

            offset += rc;
            if (offset < buffer.length) {
                if (sleepMS >= timeoutMS) {
                    throw (new IOException(
                            "Read buffer operation timed out"));
                }
                Sleeper.sleepNoException(SLEEP_TIME_MS);
                sleepMS += SLEEP_TIME_MS;
            }

        }
        return buffer.length;
    }
    /**
     * Read data from socket into byte array
     * 
     * @param s socket to read data from
     * @param buffer byte array to put data into
     * @return number of bytes read
     * @throws IOException if unable to read data
     */
    public static int readBuffer(Socket s, byte[] buffer)
            throws IOException {
        return readBufferIS(s.getInputStream(), buffer);
    }
    /**
     * Fully populates buffer from input stream.
     * 
     * @param inputStream stream to be read to fill buffer
     * @param buffer byte array to be fully populated from input stream
     * @return number of bytes read and loaded into buffer (should always be buffer length or -1 if end of stream was encountered)
     * @throws IOException if error is encountered reading from stream
     */
    public static int readBufferIS(InputStream inputStream, byte[] buffer)
            throws IOException {
        int offset = 0;
        int length = buffer.length;
        while (offset < buffer.length) {

            int rc = inputStream.read(buffer, offset, length);
            if (rc == -1) {
                return -1;
            }

            offset += rc;
            length -= rc;

        }
        return buffer.length;
    }
}

Related Tutorials