从Socket读取一个字节数组

我已经阅读了太阳指南和不同的类似问题。 我想从Socket中读取一些未知数量的字节到数组中。

我有两个选项,我可以在循环中使用read(byte)并将字节添加到byte数组中,或者我可以使用DataInputStream readFully(byte[])将所有字节读入字节数组。 哪一个更好,如何预先找到字节数组的大小进行分配? 另外,如果我使用第一种方法如何在字节数组中追加字节。

 while(in.read(b) ! = -1) { //Add to byte array, but how to append at the end? } 

我可以使用StringBuilder接收数据,附加到它然后执行toString().getBytes()

读入小块并将其写入ByteArrayOutputStream

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buffer[] = new byte[1024]; for(int s; (s=in.read(buffer)) != -1; ) { baos.write(buffer, 0, s); } byte result[] = baos.toByteArray(); 

我有两个选项,我可以在循环中使用read(byte)并将字节添加到byte数组中,或者我可以使用DataInputStream readFully(byte [])将所有字节读入字节数组。 哪一个更好

两者都没有真正解决你的问题,因为……

如何预先找到字节数组的大小以进行分配?

…您无法预先找到字节数组的大小,除非:

  • 客户端通过在字节本身之前发送计数来告诉您有多少字节,或者
  • 您对准备接收的字节数进行限制,并将其用作数组大小。

我可以使用StringBuilder来接收数据……

理论上可以使用StringBuilder 。 但是,它需要从字节转换为字符再返回……这可能是有损的 。 您可能能够找到一个没有损耗的8位字符编码/解码…但我不会这样做。

我只是使用ByteArrayOutputStream作为缓冲区来累积字节,并使用toByteArray()来提取它们。 如@ kan的答案所示,您可以从块中读取套接字中的字节。

首先传输大小,然后传输数据?

否则,您不能使用数组,但需要使用可resize的内容。

你无法在Java中附加到数组 – 它们无法增长。 您可以设置缓冲区,并记住您当前使用它的位置。 这实际上是ArrayList的作用:分配缓冲区,当超出容量时,分配一个更大的缓冲区并复制数据。 重复。

这应该可以解决问题。 我修改了一段时间以修复实现中的一些危险的同步错误。 它不是Java类路径的一部分,但它是高效的并且动态扩展。 与其他解决方案相比,它有一些巧妙的技巧。

 /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Circular Byte Buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Copyright (C) 2002 Stephen Ostermiller https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* http://ostermiller.org/contact.pl?regarding=Java+Utilities https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This program is free software; you can redistribute it and/or modify https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* it under the terms of the GNU General Public License as published by https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the Free Software Foundation; either version 2 of the License, or https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* (at your option) any later version. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This program is distributed in the hope that it will be useful, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* but WITHOUT ANY WARRANTY; without even the implied warranty of https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* GNU General Public License for more details. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* See COPYING.TXT for details. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ package misc; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.BufferOverflowException; import com.Ostermiller.util.CircularCharBuffer; import com.Ostermiller.util.CircularObjectBuffer; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Implements the Circular Buffer producer/consumer model for bytes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* More information about this class is available from ostermiller.org. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* 

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Using this class is a simpler alternative to using a PipedInputStream https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* and a PipedOutputStream. PipedInputStreams and PipedOutputStreams don't support the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* mark operation, don't allow you to control buffer sizes that they use, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* and have a more complicated API that requires instantiating two https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* classes and connecting them. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This class is thread safe. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @see CircularCharBuffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @see CircularObjectBuffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public class CircularByteBuffer { /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The default size for a circular byte buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private final static int DEFAULT_SIZE = 1024; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* A buffer that will grow as things are added. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public final static int INFINITE_SIZE = -1; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The circular buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The actual capacity of the buffer is one less than the actual length https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* of the buffer so that an empty and a full buffer can be https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* distinguished. An empty buffer will have the markPostion and the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* writePosition equal to each other. A full buffer will have https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the writePosition one less than the markPostion. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* There are three important indexes into the buffer: https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The readPosition, the writePosition, and the markPosition. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the InputStream has never been marked, the readPosition and https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the markPosition should always be the same. The bytes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* available to be read go from the readPosition to the writePosition, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* wrapping around the end of the buffer. The space available for writing https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* goes from the write position to one less than the markPosition, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* wrapping around the end of the buffer. The bytes that have https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* been saved to support a reset() of the InputStream go from markPosition https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* to readPosition, wrapping around the end of the buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected byte[] buffer; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Index of the first byte available to be read. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected volatile int readPosition = 0; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Index of the first byte available to be written. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected volatile int writePosition = 0; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Index of the first saved byte. (To support stream marking.) https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected volatile int markPosition = 0; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Number of bytes that have to be saved https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* to support mark() and reset() on the InputStream. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected volatile int markSize = 0; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If this buffer is infinite (should resize itself when full) https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected volatile boolean infinite = false; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* True if a write to a full buffer should block until the buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* has room, false if the write method should throw an IOException https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected boolean blockingWrite = true; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The InputStream that can empty this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected InputStream in = new CircularByteBufferInputStream(); /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* true if the close() method has been called on the InputStream https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected boolean inputStreamClosed = false; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The OutputStream that can fill this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected OutputStream out = new CircularByteBufferOutputStream(); /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* true if the close() method has been called on the OutputStream https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected boolean outputStreamClosed = false; /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Make this buffer ready for reuse. The contents of the buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* will be cleared and the streams associated with this buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* will be reopened if they had been closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public void clear(){ synchronized (this){ readPosition = 0; writePosition = 0; markPosition = 0; outputStreamClosed = false; inputStreamClosed = false; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Retrieve a OutputStream that can be used to fill https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Write methods may throw a BufferOverflowException if https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the buffer is not large enough. A large enough buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* size must be chosen so that this does not happen or https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the caller must be prepared to catch the exception and https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* try again once part of the buffer has been consumed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the producer for this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public OutputStream getOutputStream(){ return out; } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Retrieve a InputStream that can be used to empty https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This InputStream supports marks at the expense https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* of the buffer size. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the consumer for this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public InputStream getInputStream(){ return in; } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Get number of bytes that are available to be read. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that the number of bytes available plus https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the number of bytes free may not add up to the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* capacity of this buffer, as the buffer may reserve some https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* space for other purposes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the size in bytes of this buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public int getAvailable(){ synchronized (this){ return available(); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Get the number of bytes this buffer has free for https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* writing. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that the number of bytes available plus https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the number of bytes free may not add up to the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* capacity of this buffer, as the buffer may reserve some https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* space for other purposes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the available space in bytes of this buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public int getSpaceLeft(){ synchronized (this){ return spaceLeft(); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Get the capacity of this buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that the number of bytes available plus https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the number of bytes free may not add up to the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* capacity of this buffer, as the buffer may reserve some https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* space for other purposes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the size in bytes of this buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public int getSize(){ synchronized (this){ return buffer.length; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* double the size of the buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private void resize(){ synchronized (CircularByteBuffer.this) { byte[] newBuffer = new byte[buffer.length https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* 2]; int marked = marked(); int available = available(); if (markPosition <= writePosition){ // any space between the mark and // the first write needs to be saved. // In this case it is all in one piece. int length = writePosition - markPosition; System.arraycopy(buffer, markPosition, newBuffer, 0, length); } else { int length1 = buffer.length - markPosition; System.arraycopy(buffer, markPosition, newBuffer, 0, length1); int length2 = writePosition; System.arraycopy(buffer, 0, newBuffer, length1, length2); } buffer = newBuffer; markPosition = 0; readPosition = marked; writePosition = marked + available; CircularByteBuffer.this.notifyAll(); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Space available in the buffer which can be written. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private int spaceLeft(){ if (writePosition < markPosition){ // any space between the first write and // the mark except one byte is available. // In this case it is all in one piece. return (markPosition - writePosition - 1); } // space at the beginning and end. return ((buffer.length - 1) - (writePosition - markPosition)); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Bytes available for reading. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private int available(){ if (readPosition <= writePosition){ // any space between the first read and // the first write is available. In this case i // is all in one piece. return (writePosition - readPosition); } // space at the beginning and end. return (buffer.length - (readPosition - writePosition)); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Bytes saved for supporting marks. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private int marked(){ if (markPosition <= readPosition){ // any space between the markPosition and // the first write is marked. In this case i // is all in one piece. return (readPosition - markPosition); } // space at the beginning and end. return (buffer.length - (markPosition - readPosition)); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If we have passed the markSize reset the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* mark so that the space can be used. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ private void ensureMark(){ if (marked() >= markSize){ markPosition = readPosition; markSize = 0; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Create a new buffer with a default capacity. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Writing to a full buffer will block until space https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* is available rather than throw an exception. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public CircularByteBuffer(){ this (DEFAULT_SIZE, true); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Create a new buffer with given capacity. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Writing to a full buffer will block until space https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* is available rather than throw an exception. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that the buffer may reserve some bytes for https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* special purposes and capacity number of bytes may https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* not be able to be written to the buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that if the buffer is of INFINITE_SIZE it will https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* neither block or throw exceptions, but rather grow https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* without bound. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param size desired capacity of the buffer in bytes or CircularByteBuffer.INFINITE_SIZE. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public CircularByteBuffer(int size){ this (size, true); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Create a new buffer with a default capacity and https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* given blocking behavior. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param blockingWrite true writing to a full buffer should block https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* until space is available, false if an exception should https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* be thrown instead. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public CircularByteBuffer(boolean blockingWrite){ this (DEFAULT_SIZE, blockingWrite); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Create a new buffer with the given capacity and https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* blocking behavior. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that the buffer may reserve some bytes for https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* special purposes and capacity number of bytes may https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* not be able to be written to the buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Note that if the buffer is of INFINITE_SIZE it will https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* neither block or throw exceptions, but rather grow https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* without bound. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param size desired capacity of the buffer in bytes or CircularByteBuffer.INFINITE_SIZE. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param blockingWrite true writing to a full buffer should block https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* until space is available, false if an exception should https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* be thrown instead. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ public CircularByteBuffer(int size, boolean blockingWrite){ if (size == INFINITE_SIZE){ buffer = new byte[DEFAULT_SIZE]; infinite = true; } else { buffer = new byte[size]; infinite = false; } this.blockingWrite = blockingWrite; } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Class for reading from a circular byte buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected class CircularByteBufferInputStream extends InputStream { /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Returns the number of bytes that can be read (or skipped over) from this https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* input stream without blocking by the next caller of a method for this input https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* stream. The next caller might be the same thread or or another thread. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return the number of bytes that can be read from this input stream without blocking. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public int available() throws IOException { synchronized (CircularByteBuffer.this){ if (inputStreamClosed) throw new IOException("InputStream has been closed, it is not ready."); return (CircularByteBuffer.this.available()); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Close the stream. Once a stream has been closed, further read(), available(), https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* mark(), or reset() invocations will throw an IOException. Closing a https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* previously-closed stream, however, has no effect. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException never. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void close() throws IOException { synchronized (CircularByteBuffer.this){ inputStreamClosed = true; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Mark the present position in the stream. Subsequent calls to reset() will https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* attempt to reposition the stream to this point. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*

https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The readAheadLimit must be less than the size of circular buffer, otherwise https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* this method has no effect. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param readAheadLimit Limit on the number of bytes that may be read while https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* still preserving the mark. After reading this many bytes, attempting to https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* reset the stream will fail. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void mark(int readAheadLimit) { synchronized (CircularByteBuffer.this){ //if (inputStreamClosed) throw new IOException("InputStream has been closed; cannot mark a closed InputStream."); if (buffer.length - 1 > readAheadLimit) { markSize = readAheadLimit; markPosition = readPosition; } } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Tell whether this stream supports the mark() operation. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return true, mark is supported. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public boolean markSupported() { return true; } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Read a single byte. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This method will block until a byte is available, an I/O error occurs, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* or the end of the stream is reached. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return The byte read, as an integer in the range 0 to 255 (0x00-0xff), https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* or -1 if the end of the stream has been reached https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public int read() throws IOException { synchronized (CircularByteBuffer.this){ while (true) { if (inputStreamClosed) throw new IOException("InputStream has been closed; cannot read from a closed InputStream."); int available = CircularByteBuffer.this.available(); if (available > 0){ int result = buffer[readPosition] & 0xff; readPosition++; if (readPosition == buffer.length){ readPosition = 0; } ensureMark(); return result; } else if (outputStreamClosed){ return -1; } else { try { CircularByteBuffer.this.wait(); } catch(Exception x){ throw new IOException("Blocking read operation interrupted."); } } } } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Read bytes into an array. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This method will block until some input is available, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* an I/O error occurs, or the end of the stream is reached. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param cbuf Destination buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return The number of bytes read, or -1 if the end of https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the stream has been reached https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public int read(byte[] cbuf) throws IOException { return read(cbuf, 0, cbuf.length); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Read bytes into a portion of an array. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This method will block until some input is available, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* an I/O error occurs, or the end of the stream is reached. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param cbuf Destination buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param off Offset at which to start storing bytes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param len Maximum number of bytes to read. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return The number of bytes read, or -1 if the end of https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* the stream has been reached https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public int read(byte[] cbuf, int off, int len) throws IOException { synchronized (CircularByteBuffer.this){ while (true){ if (inputStreamClosed) throw new IOException("InputStream has been closed; cannot read from a closed InputStream."); int available = CircularByteBuffer.this.available(); if (available > 0){ int length = Math.min(len, available); int firstLen = Math.min(length, buffer.length - readPosition); int secondLen = length - firstLen; System.arraycopy(buffer, readPosition, cbuf, off, firstLen); if (secondLen > 0){ System.arraycopy(buffer, 0, cbuf, off+firstLen, secondLen); readPosition = secondLen; } else { readPosition += length; } if (readPosition == buffer.length) { readPosition = 0; } ensureMark(); return length; } else if (outputStreamClosed){ return -1; } else { try { CircularByteBuffer.this.wait(); } catch(Exception x){ throw new IOException("Blocking read operation interrupted."); } } } } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Reset the stream. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the stream has been marked, then attempt to reposition i https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* at the mark. If the stream has not been marked, or more bytes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* than the readAheadLimit have been read, this method has no effect. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void reset() throws IOException { synchronized (CircularByteBuffer.this){ if (inputStreamClosed) throw new IOException("InputStream has been closed; cannot reset a closed InputStream."); readPosition = markPosition; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Skip bytes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This method will block until some bytes are available, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* an I/O error occurs, or the end of the stream is reached. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param n The number of bytes to skip https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @return The number of bytes actually skipped https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IllegalArgumentException if n is negative. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public long skip(long n) throws IOException, IllegalArgumentException { synchronized (CircularByteBuffer.this){ while (true){ if (inputStreamClosed) throw new IOException("InputStream has been closed; cannot skip bytes on a closed InputStream."); int available = CircularByteBuffer.this.available(); if (available > 0){ int length = Math.min((int)n, available); int firstLen = Math.min(length, buffer.length - readPosition); int secondLen = length - firstLen; if (secondLen > 0){ readPosition = secondLen; } else { readPosition += length; } if (readPosition == buffer.length) { readPosition = 0; } ensureMark(); return length; } else if (outputStreamClosed){ return 0; } else { try { CircularByteBuffer.this.wait(); } catch(Exception x){ throw new IOException("Blocking read operation interrupted."); } } } } } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Class for writing to a circular byte buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the buffer is full, the writes will either block https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* until there is some space available or throw an IOException https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* based on the CircularByteBuffer's preference. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ protected class CircularByteBufferOutputStream extends OutputStream { /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Close the stream, flushing it first. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* This will cause the InputStream associated with this circular buffer https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* to read its last bytes once it empties the buffer. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Once a stream has been closed, further write() or flush() invocations https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* will cause an IOException to be thrown. Closing a previously-closed stream, https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* however, has no effect. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException never. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void close() throws IOException { synchronized (CircularByteBuffer.this){ if (!outputStreamClosed){ flush(); } outputStreamClosed = true; } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Flush the stream. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void flush() throws IOException { synchronized (CircularByteBuffer.this) { // this method needs to do nothing CircularByteBuffer.this.notifyAll(); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Write an array of bytes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the buffer allows blocking writes, this method will block until https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* all the data has been written rather than throw an IOException. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param cbuf Array of bytes to be written https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws BufferOverflowException if buffer does not allow blocking writes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* and the buffer is full. If the exception is thrown, no data https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* will have been written since the buffer was set to be non-blocking. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed, or the write is interrupted. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void write(byte[] cbuf) throws IOException { write(cbuf, 0, cbuf.length); } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Write a portion of an array of bytes. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the buffer allows blocking writes, this method will block until https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* all the data has been written rather than throw an IOException. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param cbuf Array of bytes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param off Offset from which to start writing bytes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param len - Number of bytes to write https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws BufferOverflowException if buffer does not allow blocking writes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* and the buffer is full. If the exception is thrown, no data https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* will have been written since the buffer was set to be non-blocking. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed, or the write is interrupted. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void write(byte[] cbuf, int off, int len) throws IOException { synchronized (CircularByteBuffer.this){ while (len > 0){ if (outputStreamClosed) throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream."); if (inputStreamClosed) throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer."); while (infinite && spaceLeft() < len){ resize(); } while (!infinite && spaceLeft() < len) { // wait for space to become available try { CircularByteBuffer.this.wait(); } catch (InterruptedException e) { } } if (!blockingWrite && spaceLeft() < len) throw new BufferOverflowException(); int realLen = Math.min(len, spaceLeft()); int firstLen = Math.min(realLen, buffer.length - writePosition); int secondLen = Math.min(realLen - firstLen, buffer.length - markPosition - 1); int written = firstLen + secondLen; if (firstLen > 0){ System.arraycopy(cbuf, off, buffer, writePosition, firstLen); } if (secondLen > 0){ System.arraycopy(cbuf, off+firstLen, buffer, 0, secondLen); writePosition = secondLen; } else { writePosition += written; } if (writePosition == buffer.length) { writePosition = 0; } off += written; len -= written; } CircularByteBuffer.this.notifyAll(); } } /https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* Write a single byte. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* The byte to be written is contained in the 8 low-order bits of the https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* given integer value; the 24 high-order bits are ignored. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* If the buffer allows blocking writes, this method will block until https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* all the data has been written rather than throw an IOException. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @param c number of bytes to be written https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws BufferOverflowException if buffer does not allow blocking writes https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* and the buffer is full. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @throws IOException if the stream is closed, or the write is interrupted. https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/* @since ostermillerutils 1.00.00 https://stackoverflow.com/questions/8274966/reading-a-byte-array-from-socket/*/ @Override public void write(int c) throws IOException { boolean written = false; synchronized (CircularByteBuffer.this){ while (!written){ if (outputStreamClosed) throw new IOException("OutputStream has been closed; cannot write to a closed OutputStream."); if (inputStreamClosed) throw new IOException("Buffer closed by InputStream; cannot write to a closed buffer."); int spaceLeft = spaceLeft(); while (infinite && spaceLeft < 1){ resize(); spaceLeft = spaceLeft(); } if (!blockingWrite && spaceLeft < 1) throw new BufferOverflowException(); if (spaceLeft > 0){ buffer[writePosition] = (byte)(c & 0xff); writePosition++; if (writePosition == buffer.length) { writePosition = 0; } written = true; } } if (!written){ try { CircularByteBuffer.this.wait(100); } catch(Exception x){ throw new IOException("Waiting for available space in buffer interrupted."); } } CircularByteBuffer.this.notifyAll(); } } } }

从ostermiller.org获取jar并将其添加到项目的库中以获取其他缺失的类。