1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 8 28 public class DataOutputBuffer extends DataOutputStream { 29 30 private static class Buffer extends ByteArrayOutputStream { 31 public byte[] getData() { return buf; } 32 public int getLength() { return count; } 33 public void reset() { count = 0; } 34 35 public void write(DataInput in, int len) throws IOException { 36 int newcount = count + len; 37 if (newcount > buf.length) { 38 byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; 39 System.arraycopy(buf, 0, newbuf, 0, count); 40 buf = newbuf; 41 } 42 in.readFully(buf, count, len); 43 count = newcount; 44 } 45 } 46 47 private Buffer buffer; 48 49 50 public DataOutputBuffer() { 51 this(new Buffer()); 52 } 53 54 private DataOutputBuffer(Buffer buffer) { 55 super(buffer); 56 this.buffer = buffer; 57 } 58 59 62 public byte[] getData() { return buffer.getData(); } 63 64 65 public int getLength() { return buffer.getLength(); } 66 67 68 public DataOutputBuffer reset() { 69 this.written = 0; 70 buffer.reset(); 71 return this; 72 } 73 74 75 public void write(DataInput in, int length) throws IOException { 76 buffer.write(in, length); 77 } 78 } 79 | Popular Tags |