1 23 24 package com.sun.enterprise.admin.jmx.remote.streams; 25 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.io.DataInputStream ; 29 30 public class JMXChunkedInputStream extends InputStream { 31 private InputStream in = null; 32 private byte[] buffer = null; 33 private int remainderInBuf = 0; 34 private int remainderInChannel = 0; 35 private int idx = 0; 36 private boolean EOF = false; 37 38 public JMXChunkedInputStream(InputStream in) { 39 this.in = in; 40 } 41 42 public void close() throws IOException { 43 in.close(); 44 } 45 46 public int available() { 47 return remainderInBuf; 48 } 49 50 public boolean markSupported() { 51 return false; 52 } 53 54 public void mark(int readLimit) { 55 return; 56 } 57 58 public void reset() throws IOException { 59 throw (new IOException ("markSupported = false")); 60 } 61 62 private void readObject() throws IOException { 63 try { 64 DataInputStream dI = new DataInputStream (in); 65 int len = 0; 66 if (remainderInChannel > 0) 67 len = remainderInChannel; 68 else { 69 remainderInChannel = len = dI.readInt(); 70 if (len <= 0) { 71 setEOF(); 72 return; 73 } 74 } 75 76 buffer = new byte[len]; 77 remainderInBuf = dI.read(buffer, 0, len); 78 if (remainderInBuf == -1) { 79 setEOF(); 80 return; 81 } 82 remainderInChannel -= remainderInBuf; 83 idx = 0; 84 } catch (IOException ioe) { 85 if (ioe instanceof java.io.EOFException || 86 ioe.getMessage().indexOf("EOF") != -1) { 87 setEOF(); 88 return; 89 } else 90 throw ioe; 91 } 92 } 93 94 private void setEOF() { 95 EOF = true; 96 buffer = null; 97 remainderInBuf = 0; 98 idx = 0; 99 } 100 101 public int read() throws IOException { 102 if (EOF) 103 return -1; 104 byte ret = -1; 105 if (remainderInBuf == 0) 106 readObject(); 107 if (EOF) 108 return -1; 109 ret = buffer[idx++]; 110 remainderInBuf--; 111 return ret; 112 } 113 114 public int read(byte[] b) throws IOException { 115 if (b == null) 116 throw (new NullPointerException ("byte array is null")); 117 return read(b, 0, b.length); 118 } 119 120 public int read(byte[] b, int off, int len) throws IOException { 121 if (EOF) 122 return -1; 123 if (b == null) 124 throw (new NullPointerException ("byte array is null")); 125 if (off < 0 || len < 0 || (off+len) > b.length) 126 throw (new IndexOutOfBoundsException ( 127 "offset="+off+ 128 ", len="+len+ 129 ", (off+len)="+(off+len)+ 130 ", b.length="+b.length+ 131 ", (off+len)>b.length="+ 132 ((off+len)>b.length))); 133 if (len == 0) 134 return 0; 135 136 if (remainderInBuf == 0) 137 readObject(); 138 if (EOF) 139 return -1; 140 int len1 = (len > remainderInBuf) ? remainderInBuf : len; 141 System.arraycopy(buffer, idx, b, off, len1); 142 idx += len1; 143 remainderInBuf -= len1; 144 return len1; 145 } 146 147 public long skip(int n) throws IOException { 148 if (EOF) 149 return 0; 150 if (n <= 0) 151 return 0; 152 if (remainderInBuf >= n) { 153 idx += n; 154 remainderInBuf -= n; 155 return n; 156 } 157 int skipped = remainderInBuf; 158 n -= skipped; 159 remainderInBuf = 0; 160 idx = 0; 161 readObject(); 162 if (EOF) 163 return skipped; 164 idx += n; 165 remainderInBuf -= n; 166 skipped += n; 167 return skipped; 168 } 169 } 170 171 | Popular Tags |