1 18 19 package org.apache.activemq.transport.tcp; 20 21 import java.io.EOFException ; 22 import java.io.FilterOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 31 32 public class TcpBufferedOutputStream extends FilterOutputStream { 33 private final static int BUFFER_SIZE = 8192; 34 private byte[] buffer; 35 private int bufferlen; 36 private int count; 37 private boolean closed; 38 39 44 public TcpBufferedOutputStream(OutputStream out) { 45 this(out, BUFFER_SIZE); 46 } 47 48 56 public TcpBufferedOutputStream(OutputStream out, int size) { 57 super(out); 58 if (size <= 0) { 59 throw new IllegalArgumentException ("Buffer size <= 0"); 60 } 61 buffer = new byte[size]; 62 bufferlen=size; 63 } 64 65 71 public void write(int b) throws IOException { 72 if ((bufferlen-count) < 1) { 73 flush(); 74 } 75 buffer[count++] = (byte) b; 76 } 77 78 79 87 public void write(byte b[], int off, int len) throws IOException { 88 if ((bufferlen-count) < len) { 89 flush(); 90 } 91 if (buffer.length >= len) { 92 System.arraycopy(b, off, buffer, count, len); 93 count += len; 94 } 95 else { 96 out.write(b, off, len); 97 } 98 } 99 100 107 public void flush() throws IOException { 108 if (count > 0 && out != null) { 109 out.write(buffer, 0, count); 110 count = 0; 111 } 112 } 113 114 119 public void close() throws IOException { 120 super.close(); 121 closed = true; 122 } 123 124 125 130 private final void checkClosed() throws IOException { 131 if (closed) { 132 throw new EOFException ("Cannot write to the stream any more it has already been closed"); 133 } 134 } 135 136 } 137 | Popular Tags |