1 23 24 package com.sun.enterprise.admin.jmx.remote.streams; 25 26 import java.io.IOException ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.DataOutputStream ; 29 import java.io.OutputStream ; 30 import java.io.Serializable ; 31 32 public class JMXChunkedOutputStream extends OutputStream { 33 private OutputStream out = null; 34 private byte[] buffer = null; 35 private int bufCount = 0; 36 37 public JMXChunkedOutputStream(OutputStream out) { 38 this.out = out; 39 buffer = new byte[8192]; 40 } 41 42 public void close() throws IOException { 43 if (bufCount > 0) 44 flush(); 45 out.close(); 46 } 47 48 public void flush() throws IOException { 49 if (bufCount > 0) 50 flushBuffer(); 51 else 52 out.flush(); 53 } 54 55 private void flushBuffer() throws IOException { 56 writeObject(buffer, 0, bufCount); 57 bufCount = 0; 58 } 59 60 public void writeEOF(int padLen) throws IOException { 61 DataOutputStream dO = new DataOutputStream (out); 62 dO.writeInt(0); 63 dO.write(new byte[padLen],0,padLen); 69 dO.flush(); 70 } 71 72 public void write(byte[] b) throws IOException { 73 if (b == null) 74 throw (new NullPointerException ("byte array is null")); 75 write(b, 0, b.length); 76 } 77 78 public void write(byte[] b, int off, int len) throws IOException { 79 if (b == null) 80 throw (new NullPointerException ("byte array is null")); 81 if (off < 0 || len < 0 || (off+len) > b.length) 82 throw (new IndexOutOfBoundsException ( 83 "offset="+off+ 84 ", len="+len+ 85 ", (off+len)="+(off+len)+ 86 ", b.length="+b.length+ 87 ", (off+len)>b.length="+ 88 ((off+len)>b.length))); 89 if (len == 0) 90 return; 91 if (bufCount > 0 && (bufCount+len) >= 8192) { 92 flushBuffer(); 93 } 94 if (len >= 8192) { 95 writeObject(b, off, len); 96 return; 97 } 98 writeBuffer(b, off, len); 99 } 100 101 public void write(int by) throws IOException { 102 byte b = (byte) by; 103 if (bufCount > 0 && (bufCount+1) >= 8192) { 104 flushBuffer(); 105 } 106 buffer[bufCount] = b; 107 bufCount++; 108 } 109 110 private void writeBuffer(byte[] b, int off, int len) { 111 System.arraycopy(b, off, buffer, bufCount, len); 112 bufCount += len; 113 } 114 115 private void writeObject(byte[] b, int off, int len) 116 throws IOException { 117 DataOutputStream dO = new DataOutputStream (out); 118 dO.writeInt(len); 119 dO.write(b, off, len); 120 dO.flush(); 121 } 122 } 123 124 | Popular Tags |