1 32 package com.imagero.uio.io; 33 34 import com.imagero.uio.Sys; 35 import com.imagero.uio.buffer.Buffer; 36 import com.imagero.uio.buffer.BufferManager; 37 import com.imagero.uio.buffer.ByteBuffer; 38 import com.imagero.uio.buffer.DefaultBufferManager; 39 40 import java.io.EOFException ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.util.Vector ; 45 46 52 public class MSInputStream extends InputStream { 53 54 BufferManager bufferManager; 55 56 int vpos; 57 int vcount; 58 int totalRead; 59 int totalCount; 60 61 protected byte buf[]; 62 63 protected int pos; 64 protected int count; 65 protected int mark = 0; 66 67 72 public MSInputStream(Vector v) { 73 Buffer [] ds = new Buffer[v.size()]; 74 for (int i = 0; i < ds.length; i++) { 75 ds[i] = new ByteBuffer((byte[]) v.elementAt(i)); 76 } 77 this.bufferManager = new DefaultBufferManager(ds); 78 79 this.vcount = v.size(); 80 this.totalCount = countBytes(); 81 nextArray(); 82 } 83 84 89 public MSInputStream(Buffer[] ds) { 90 this(new DefaultBufferManager(ds)); 91 } 92 93 public MSInputStream(BufferManager sourceManager) { 94 this.bufferManager = sourceManager; 95 this.vcount = sourceManager.getCount(); 96 this.totalCount = countBytes(); 97 nextArray(); 98 } 99 100 protected int countBytes() { 101 int count = 0; 102 for(int i = 0; i < bufferManager.getCount(); i++) { 103 count += bufferManager.getDataLength(i); 104 } 105 return count; 106 } 107 108 protected void nextArray() { 109 try { 111 this.buf = bufferManager.getData(vpos++); 112 this.pos = 0; 113 this.count = buf.length; 114 } 115 catch(Exception ex) { 116 this.buf = null; 118 this.pos = 0; 119 this.count = 0; 120 } 121 } 122 123 public int read(byte b[], int off, int len) throws IOException { 124 if(totalRead >= totalCount) { throw new EOFException (); 126 } 127 128 int i = 0; 129 for(; i < len; i++) { 130 int c = read(); 131 if(c == -1) { 132 break; 133 } 134 b[off + i] = (byte) c; 135 } 136 return i; 137 } 138 139 public int read() throws IOException { 140 if(totalRead < totalCount) { 141 if(pos == count) { 142 nextArray(); 143 if(buf == null) { 144 return -1; 145 } 146 } 148 totalRead++; 149 return buf[pos++] & 0xFF; 150 } 151 return -1; 152 } 153 154 public static void printHex(int value) { 155 value = value & 0xFF; 156 String s = Integer.toHexString(value); 157 if(s.length() == 1) { 158 Sys.out.print("0"); 159 } 160 Sys.out.print(s); 161 Sys.out.print(" "); 162 } 163 164 public synchronized long skip(long n) { 165 int skipped = 0; 166 while(n + pos > count) { 167 int skp = count - pos; 168 169 n -= skp; 170 skipped += skp; 171 totalRead += skp; 172 173 nextArray(); 174 if(buf == null) { 175 return skipped; 176 } 177 } 178 return skipped; 179 } 180 181 public int available() { 182 return totalCount - totalRead; 183 } 184 185 public void mark(int readAheadLimit) { 186 mark = totalRead; 187 } 188 189 protected synchronized void gotoAbsPos(int absPos) throws IOException { 190 int i = 0; 191 int count = 0; 192 193 int size = bufferManager.getCount(); 194 for(; i < size; i++) { 195 int length = bufferManager.getDataLength(i); 196 if((count + length) < absPos) { 197 count += length; 198 } 199 else { 200 break; 201 } 202 } 203 204 this.buf = bufferManager.getData(i); 205 pos = absPos - count; 206 totalRead = absPos; 207 } 208 209 public void reset() throws IOException { 210 gotoAbsPos(mark); 211 } 212 213 public boolean markSupported() { 214 return true; 215 } 216 217 public void debug(int len) throws IOException { 218 byte[] b = this.buf; 219 int p = pos; 220 int l = len; 221 222 224 if((p + len) > b.length) { 225 len = b.length - p; 226 } 227 228 if(len == 0) { 229 b = bufferManager.getData(vpos); 230 len = l; 231 } 232 233 235 for(int i = 0; i < len; i++) { 236 int c = b[i + p] & 0xFF; 237 printHex(c); 238 } 239 } 240 241 242 public void debug() throws IOException { 243 int size = bufferManager.getCount(); 244 for(int i = 0; i < size; i++) { 245 byte[] b = bufferManager.getData(i); 246 for(int j = 0; j < b.length; j++) { 248 int c = b[j] & 0xFF; 249 printHex(c); 250 } 251 } 252 } 253 254 public void debug(OutputStream out) throws IOException { 255 int size = bufferManager.getCount(); 256 for(int i = 0; i < size; i++) { 257 byte[] b = bufferManager.getData(i); 258 for(int j = 0; j < b.length; j++) { 259 int c = b[j] & 0xFF; 260 out.write(c); 261 } 262 } 263 } 264 } 265 266 267 268 269 270 271 | Popular Tags |