Java Utililty Methods Socket Read

List of utility methods to do Socket Read

Description

The list of methods to do Socket Read are organized into topic(s).

Method

Stringdump(final Socket socket)
Dumps the state of a socket
if (socket == null)
    return "Socket [null]";
final StringBuilder b = new StringBuilder("Socket [");
b.append("\n\tConnected:").append(socket.isConnected());
final SocketAddress lsa = socket.getLocalSocketAddress();
final SocketAddress sa = socket.getRemoteSocketAddress();
if (sa != null && (sa instanceof InetSocketAddress)) {
    final InetSocketAddress isa = (InetSocketAddress) sa;
...
BufferedReadergetBufferedReader(Socket s)
get Buffered Reader
return new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReadergetBufferedReaderFromInputStream(Socket socket)
get Buffered Reader From Input Stream
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
InputStreamgetInputStream(Socket socket)
get Input Stream
try {
    return socket.getInputStream();
} catch (IOException e) {
    e.printStackTrace();
return null;
InputStreamgetInputStreamFromSocket(final Socket socket)
Returns a InputStream from the specified socket
return socket.getInputStream();
PrintWritergetPrintWriter(Socket s)
get Print Writer
return new PrintWriter(s.getOutputStream());
PrintWritergetPrintWriterFromOutputStream(Socket socket)
get Print Writer From Output Stream
return new PrintWriter(socket.getOutputStream());
StringgetResponse(Socket socket)
get Response
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = in.readLine();
return response;
byte[]readBytesIntoSocket(Socket socket)
read Bytes Into Socket
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);
while ((count = dis.read(buffer)) > 0) {
...
intreadData(Socket connId, byte[] dataRead, int nchar)
Reads nchar from a given sockets, blocks on read untill nchar are read of conenction has error bRead returns the bytes read
InputStream input;
input = connId.getInputStream();
int nread = 0;
int startTime = (int) (new Date().getTime());
do {
    if (input.available() != 0) {
        nread += input.read(dataRead, nread, nchar - nread);
        startTime = (int) (new Date().getTime());
...