FastByteArrayOutputStream
作者:anotherbug 日期:2007-09-18 09:58:15
1 | import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
public class FastByteArrayOutputStream extends OutputStream
{
private static final int DEFAULT_BLOCK_SIZE = 8192;
private LinkedList buffers;
private byte buffer[];
private boolean closed;
private int blockSize;
private int index;
private int size;
public FastByteArrayOutputStream()
{
this(8192);
}
public FastByteArrayOutputStream(int aSize)
{
blockSize = aSize;
buffer = new byte[blockSize];
}
public int getSize()
{
return size + index;
}
public void close()
{
closed = true;
}
public byte[] toByteArray()
{
byte data[] = new byte[getSize()];
int pos = 0;
if(buffers != null)
{
for(Iterator iter = buffers.iterator(); iter.hasNext();)
{
byte bytes[] = (byte[])(byte[])iter.next();
System.arraycopy(bytes, 0, data, pos, blockSize);
pos += blockSize;
}
}
System.arraycopy(buffer, 0, data, pos, index);
return data;
}
public String toString()
{
return new String(toByteArray());
}
public void write(int datum)
throws IOException
{
if(closed)
throw new IOException("Stream closed");
if(index == blockSize)
addBuffer();
buffer[index++] = (byte)datum;
}
public void write(byte data[], int offset, int length)
throws IOException
{
if(data == null)
throw new NullPointerException();
if(offset < 0 || offset + length > data.length || length < 0)
throw new IndexOutOfBoundsException();
if(closed)
throw new IOException("Stream closed");
if(index + length > blockSize)
{
do
{
if(index == blockSize)
addBuffer();
int copyLength = blockSize - index;
if(length < copyLength)
copyLength = length;
System.arraycopy(data, offset, buffer, index, copyLength);
offset += copyLength;
index += copyLength;
length -= copyLength;
} while(length > 0);
} else
{
System.arraycopy(data, offset, buffer, index, length);
index += length;
}
}
public void writeTo(OutputStream out)
throws IOException
{
if(buffers != null)
{
byte bytes[];
for(Iterator iter = buffers.iterator(); iter.hasNext(); out.write(bytes, 0, blockSize))
bytes = (byte[])(byte[])iter.next();
}
out.write(buffer, 0, index);
}
public void writeTo(RandomAccessFile out)
throws IOException
{
if(buffers != null)
{
byte bytes[];
for(Iterator iter = buffers.iterator(); iter.hasNext(); out.write(bytes, 0, blockSize))
bytes = (byte[])(byte[])iter.next();
}
out.write(buffer, 0, index);
}
public void writeTo(Writer out, String encoding)
throws IOException
{
if(buffers != null)
{
for(Iterator iter = buffers.iterator(); iter.hasNext();)
{
byte bytes[] = (byte[])(byte[])iter.next();
if(encoding != null)
out.write(new String(bytes, encoding));
else
out.write(new String(bytes));
}
}
if(encoding != null)
out.write(new String(buffer, 0, index, encoding));
else
out.write(new String(buffer, 0, index));
}
protected void addBuffer()
{
if(buffers == null)
buffers = new LinkedList();
buffers.addLast(buffer);
buffer = new byte[blockSize];
size += index;
index = 0;
}
}
|
平均得分
(0 次评分)
评论: 1 | 查看次数: 1773
- 共有 1 条评论
- 共有 1 条评论
发表评论
订阅
上一篇
|

文章来自:
标签: 





窃听器
手机监听器
监听器
手机窃听器
窃听器
手机监听器
监听器
手机窃听器
窃听器
手机监听器
监听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器
手机窃听器│窃听器