Java Socket Read readBytesIntoSocket(Socket socket)

Here you can find the source of readBytesIntoSocket(Socket socket)

Description

read Bytes Into Socket

License

Apache License

Declaration

public static byte[] readBytesIntoSocket(Socket socket) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;

import java.io.IOException;
import java.net.Socket;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 10;

    public static byte[] readBytesIntoSocket(Socket socket) throws IOException, InterruptedException {

        DataInputStream dis = new DataInputStream(socket.getInputStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int count = 0;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        while (dis.available() == 0) {
            Thread.sleep(1);/*from  w  w  w .j  a v  a2 s.co m*/
        }

        while ((count = dis.read(buffer)) > 0) {
            out.write(buffer, 0, count);
            if (count < DEFAULT_BUFFER_SIZE)
                break;
        }

        return out.toByteArray();
    }
}

Related

  1. getInputStream(Socket socket)
  2. getInputStreamFromSocket(final Socket socket)
  3. getPrintWriter(Socket s)
  4. getPrintWriterFromOutputStream(Socket socket)
  5. getResponse(Socket socket)
  6. readData(Socket connId, byte[] dataRead, int nchar)
  7. readMsg(Socket s)