2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
2013-05-28 19:29:24 +00:00
|
|
|
* Copyright (c) 2013 Ziver
|
|
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
2013-05-28 19:29:24 +00:00
|
|
|
|
2010-08-14 15:22:02 +00:00
|
|
|
package zutil.io;
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class DynamicByteArrayStream extends InputStream{
|
|
|
|
|
/** The byte array container */
|
|
|
|
|
private ArrayList<byte[]> bytes;
|
2013-12-17 19:18:14 +00:00
|
|
|
/** Current virtual size of the stream */
|
2009-02-26 17:10:57 +00:00
|
|
|
private int size;
|
2013-12-17 19:18:14 +00:00
|
|
|
/** Points the current byte array index */
|
|
|
|
|
private int arrayIndex;
|
|
|
|
|
/** Points to a local index in the current byte array */
|
|
|
|
|
private int arrayLocalIndex;
|
|
|
|
|
/** Current virtual position of the stream */
|
2009-08-22 18:18:54 +00:00
|
|
|
private int pos;
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new instance of DynamicByteArrayStream
|
|
|
|
|
*/
|
|
|
|
|
public DynamicByteArrayStream(){
|
|
|
|
|
bytes = new ArrayList<byte[]>();
|
|
|
|
|
size = 0;
|
2013-12-17 19:18:14 +00:00
|
|
|
arrayIndex = 0;
|
|
|
|
|
arrayLocalIndex = 0;
|
2009-08-22 18:18:54 +00:00
|
|
|
pos = 0;
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append an byte array to the stream
|
2012-05-01 18:23:28 +00:00
|
|
|
*
|
|
|
|
|
* @param b is the byte array to add.
|
2009-02-26 17:10:57 +00:00
|
|
|
*/
|
2012-05-01 18:23:28 +00:00
|
|
|
public synchronized void append(byte[] b){
|
2009-02-26 17:10:57 +00:00
|
|
|
bytes.add(b);
|
|
|
|
|
size += b.length;
|
|
|
|
|
}
|
2012-05-01 18:23:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append an byte array to the stream.
|
2013-12-17 19:18:14 +00:00
|
|
|
* NOTE: This function will copy data.
|
2012-05-01 18:23:28 +00:00
|
|
|
*
|
|
|
|
|
* @param b is the byte array to add
|
|
|
|
|
* @param offset is the offset in the byte array
|
|
|
|
|
* @param length is the amount of data to add
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void append(byte[] b, int offset, int length){
|
|
|
|
|
byte[] new_b = new byte[length];
|
|
|
|
|
System.arraycopy(b, offset, new_b, 0, length);
|
2013-12-17 19:18:14 +00:00
|
|
|
bytes.add(new_b);
|
2012-05-01 18:23:28 +00:00
|
|
|
size += length;
|
|
|
|
|
}
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public synchronized int read() throws IOException {
|
2009-08-22 18:18:54 +00:00
|
|
|
if(pos >= size) return -1;
|
|
|
|
|
|
2013-12-17 19:18:14 +00:00
|
|
|
int ret = bytes.get(arrayIndex)[arrayLocalIndex] & 0xff;
|
2009-08-22 18:18:54 +00:00
|
|
|
pos++;
|
2013-12-17 19:18:14 +00:00
|
|
|
arrayLocalIndex++;
|
|
|
|
|
if(arrayLocalIndex >= bytes.get(arrayIndex).length){
|
|
|
|
|
arrayIndex++;
|
|
|
|
|
arrayLocalIndex = 0;
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2009-08-22 18:18:54 +00:00
|
|
|
|
2009-02-26 17:10:57 +00:00
|
|
|
public synchronized int read(byte b[], int off, int len) {
|
2009-08-22 18:18:54 +00:00
|
|
|
if(len <= 0) return 0;
|
|
|
|
|
if(pos >= size) return -1;
|
2013-12-17 19:18:14 +00:00
|
|
|
|
|
|
|
|
int bytes_read=0;
|
2009-08-22 18:18:54 +00:00
|
|
|
if(pos+len >= size) len = size - pos;
|
2013-12-17 19:18:14 +00:00
|
|
|
for(; bytes_read<len ;bytes_read++){
|
|
|
|
|
byte[] src = bytes.get(arrayIndex);
|
|
|
|
|
// Read length is LONGER than local array
|
|
|
|
|
if(arrayLocalIndex +len-bytes_read >= src.length){
|
|
|
|
|
int length = src.length- arrayLocalIndex;
|
|
|
|
|
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
|
2009-08-22 18:18:54 +00:00
|
|
|
|
2013-12-17 19:18:14 +00:00
|
|
|
arrayLocalIndex = 0;
|
|
|
|
|
arrayIndex++;
|
2009-08-22 18:18:54 +00:00
|
|
|
bytes_read += length;
|
|
|
|
|
}
|
2013-12-17 19:18:14 +00:00
|
|
|
// Read length is SHORTER than local array
|
2009-08-22 18:18:54 +00:00
|
|
|
else{
|
2013-12-17 19:18:14 +00:00
|
|
|
int length = len-bytes_read;
|
|
|
|
|
System.arraycopy(src, arrayLocalIndex, b, off+bytes_read, length);
|
2009-08-22 18:18:54 +00:00
|
|
|
|
2013-12-17 19:18:14 +00:00
|
|
|
arrayLocalIndex += length;
|
2009-08-22 18:18:54 +00:00
|
|
|
bytes_read += length;
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-22 18:18:54 +00:00
|
|
|
pos += len;
|
|
|
|
|
return bytes_read;
|
|
|
|
|
}
|
2009-02-26 17:10:57 +00:00
|
|
|
|
2009-08-22 18:18:54 +00:00
|
|
|
public synchronized int available() {
|
|
|
|
|
return size - pos;
|
|
|
|
|
}
|
2009-02-26 17:10:57 +00:00
|
|
|
|
2009-08-22 18:18:54 +00:00
|
|
|
/**
|
|
|
|
|
* Clears this stream from the byte arrays
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void clear(){
|
|
|
|
|
size = 0;
|
|
|
|
|
reset();
|
|
|
|
|
bytes.clear();
|
|
|
|
|
}
|
2009-02-26 17:10:57 +00:00
|
|
|
|
|
|
|
|
public synchronized void reset() {
|
2013-12-17 19:18:14 +00:00
|
|
|
arrayIndex = 0;
|
|
|
|
|
arrayLocalIndex = 0;
|
2009-08-22 18:18:54 +00:00
|
|
|
pos = 0;
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
2011-09-14 17:10:11 +00:00
|
|
|
clear();
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|
2012-05-01 18:23:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return all of the buffers content as a byte array.
|
|
|
|
|
*/
|
2013-12-17 19:18:14 +00:00
|
|
|
public byte[] getBytes(){
|
2012-05-01 18:23:28 +00:00
|
|
|
byte[] data = new byte[size];
|
|
|
|
|
this.read(data, 0, size);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WARNING: This function might return a malformed String.
|
|
|
|
|
*
|
2013-12-17 19:18:14 +00:00
|
|
|
* @return all the contents of the buffers as a String.
|
2012-05-01 18:23:28 +00:00
|
|
|
*/
|
2013-12-17 19:18:14 +00:00
|
|
|
public String toString(){
|
|
|
|
|
return new String( this.getBytes() );
|
2012-05-01 18:23:28 +00:00
|
|
|
}
|
2009-02-26 17:10:57 +00:00
|
|
|
}
|