关于
java.io.ByteArrayInputStream
和
java.io.ByteArrayOutputStream
的部分笔记,这两个类相互合作借助内存来完成数据的读写和转移。本文演示代码段的执行环境基于JDK版本
1.7
。
1 2 3 4 5 6 7 8 9
|
--java.lang.Object --java.io.InputStream --java.io.ByteArrayInputStream
--java.lang.Object --java.io.InputStream --java.io.ByteArrayOutputStream
|
1 2 3 4 5
|
public ByteArrayInputStream(byte buf[]) { this.buf = buf; this.pos = 0; this.count = buf.length; }
|
1 2 3 4 5 6
|
public ByteArrayInputStream(byte buf[], int offset, int length) { this.buf = buf; this.pos = offset; this.count = Math.min(offset + length, buf.length); this.mark = offset; }
|
1 2 3
|
public synchronized int read() { return (pos < count) ? (buf[pos++] & 0xff) : -1; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public synchronized int read(byte b[], int off, int len) { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); }
if (pos >= count) { return -1; }
int avail = count - pos; if (len > avail) { len = avail; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; }
|
1 2 3 4 5 6 7 8 9
|
public synchronized long skip(long n) { long k = count - pos; if (n < k) { k = n < 0 ? 0 : n; }
pos += k; return k; }
|
1 2 3
|
public synchronized int available() { return count - pos; }
|
1 2 3
|
public boolean markSupported() { return true; }
|
1 2 3
|
public void mark(int readAheadLimit) { mark = pos; }
|
1 2 3
|
public synchronized void reset() { pos = mark; }
|
1 2
|
public void close() throws IOException { }
|
1 2 3
|
public ByteArrayOutputStream() { this(32); }
|
1 2 3 4 5 6
|
public ByteArrayOutputStream(int size) { if (size < 0) { throw new IllegalArgumentException("Negative initial size: " + size); } buf = new byte[size]; }
|
1 2 3 4 5
|
public synchronized void write(int b) { ensureCapacity(count + 1); buf[count] = (byte) b; count += 1; }
|
1 2 3 4 5 6 7 8 9
|
public synchronized void write(byte b[], int off, int len) { if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) - b.length > 0)) { throw new IndexOutOfBoundsException(); } ensureCapacity(count + len); System.arraycopy(b, off, buf, count, len); count += len; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
private void ensureCapacity(int minCapacity) { if (minCapacity - buf.length > 0) grow(minCapacity); }
private void grow(int minCapacity) { int oldCapacity = buf.length; int newCapacity = oldCapacity << 1; if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); buf = Arrays.copyOf(buf, newCapacity); }
private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
|
1 2 3
|
public synchronized void writeTo(OutputStream out) throws IOException { out.write(buf, 0, count); }
|
1 2 3
|
public synchronized void reset() { count = 0; }
|
1 2 3
|
public synchronized byte toByteArray()[] { return Arrays.copyOf(buf, count); }
|
1 2 3
|
public synchronized int size() { return count; }
|
1 2 3 4 5
|
public synchronized String toString(String charsetName) throws UnsupportedEncodingException { return new String(buf, 0, count, charsetName); }
|
1 2 3
|
public synchronized String toString() { return new String(buf, 0, count); }
|
1 2
|
public void close() throws IOException { }
|
-
【Java基础知识】IO流—内存操作流ByteArrayInputStream、ByteArrayOutputStream
[E]
-
yuleichun.
在java开发过程中什么时候使用ByteArrayInputStream和ByteArrayOuitputStream?
[E]