1 52 53 package com.go.trove.io; 54 55 import java.io.OutputStream ; 56 import java.io.IOException ; 57 58 65 public class ByteBufferOutputStream extends OutputStream { 66 private ByteBuffer mBuffer; 67 private boolean mClosed; 68 69 public ByteBufferOutputStream(ByteBuffer buffer) { 70 mBuffer = buffer; 71 } 72 73 public void write(int b) throws IOException { 74 checkIfClosed(); 75 mBuffer.append((byte)b); 76 } 77 78 public void write(byte[] bytes) throws IOException { 79 checkIfClosed(); 80 mBuffer.append(bytes); 81 } 82 83 public void write(byte[] bytes, int offset, int length) 84 throws IOException { 85 checkIfClosed(); 86 mBuffer.append(bytes, offset, length); 87 } 88 89 public void flush() throws IOException { 90 checkIfClosed(); 91 } 92 93 public void close() { 94 mClosed = true; 95 } 96 97 private void checkIfClosed() throws IOException { 98 if (mClosed) { 99 throw new IOException ("OutputStream closed"); 100 } 101 } 102 } 103 | Popular Tags |