1 32 package com.imagero.uio; 33 34 import com.imagero.uio.buffer.Buffer; 35 import com.imagero.uio.buffer.DefaultBufferManager; 36 import com.imagero.uio.buffer.MutableBufferManager; 37 import com.imagero.uio.buffer.arrays.AbstractArrayBufferManager; 38 39 import java.io.EOFException ; 40 import java.io.IOException ; 41 42 47 public class RandomAccessBuffer extends AbstractRandomAccess { 48 49 MutableBufferManager bufferManager; 50 int dataIndex; 51 int fp; 52 byte[] buf; 53 boolean dirty; 54 55 56 61 public RandomAccessBuffer(Buffer[] ds, int byteOrder) throws IOException { 62 this(new DefaultBufferManager(ds), byteOrder); 63 } 64 65 72 public RandomAccessBuffer(MutableBufferManager bufferManager, int byteOrder) throws IOException { 73 this.bufferManager = bufferManager; 74 this.buf = bufferManager.getData(0); 75 _setByteOrder(byteOrder); 76 } 77 78 79 85 public int read() { 86 if(fp < buf.length) { 87 return buf[fp++] & 0xFF; 88 } 89 else { 90 nextArray(); 91 if(buf == null) { 92 return -1; 93 } 94 return buf[fp++] & 0xFF; 95 } 96 } 97 98 protected boolean nextArray() { 99 try { 101 if(dirty) { 102 bufferManager.setDirty(dataIndex); 103 dirty = false; 104 } 105 this.buf = bufferManager.getData(++dataIndex); 106 this.fp = 0; 107 return true; 108 } 109 catch(Exception ex) { 110 this.buf = null; 112 this.fp = 0; 113 return false; 114 } 115 } 116 117 public long getFilePointer() throws IOException { 118 return bufferManager.getDataStart(dataIndex) + fp; 119 } 120 121 128 public void seek(long pos) throws IOException { 129 if(pos < 0) { 130 throw new IOException ("" + pos); 131 } 132 133 final int index = bufferManager.getIndex((int) pos); 134 if(index != dataIndex && dirty) { 135 bufferManager.setDirty(dataIndex); 136 } 137 this.dataIndex = index; 138 this.buf = bufferManager.getData(dataIndex); 139 this.fp = (int) (pos - bufferManager.getDataStart(dataIndex)); 140 } 141 142 147 public long length() { 148 return bufferManager.getLength(); 149 } 150 151 154 public void setLength(long newLength) throws IOException { 155 } 156 157 public int read(byte[] b) throws IOException { 158 return read(b, 0, b.length); 159 } 160 161 public int read(byte[] b, int off, int length) throws IOException { 162 int read = 0; 163 int _off = off; 164 int _length = length; 165 166 while(read < length) { 167 int len = readBytes(b, _off, _length); 168 if(length == -1) { 169 if(!nextArray()) { 170 return read; 171 } 172 } 173 else { 174 read += len; 175 fp += len; 176 _off += len; 177 _length -= len; 178 } 179 } 180 181 return read; 182 } 183 184 private int readBytes(byte[] b, int off, int length) { 185 int len = Math.min(length, buf.length - fp); 186 if(len <= 0) { 187 return -1; 188 } 189 System.arraycopy(buf, fp, b, off, len); 190 return len; 191 } 192 193 202 public void write(byte[] b) throws IOException { 203 write(b, 0, b.length); 204 } 205 206 219 public void write(byte[] b, int off, int length) throws IOException { 220 int write = 0; 221 int _off = off; 222 int _length = length; 223 224 while(write < length) { 225 int len = writeBytes(b, _off, _length); 226 dirty = true; 227 if(len == -1) { 228 if(!nextArray()) { 229 throw new EOFException (); 230 } 231 } 232 else { 233 write += len; 234 fp += len; 235 _off += len; 236 _length -= len; 237 if(_length == 0) { 238 break; 239 } 240 } 241 } 242 } 243 244 public void flush() throws IOException { 245 if(dirty) { 246 bufferManager.setDirty(dataIndex); 247 dirty = false; 248 } 249 bufferManager.flush(); 250 } 251 252 public void setByteOrder(int byteOrder) throws IOException { 253 super.setByteOrder(byteOrder); 254 if(bufferManager instanceof AbstractArrayBufferManager) { 255 ((AbstractArrayBufferManager) bufferManager).setByteOrder(getByteOrder()); 256 } 257 } 258 259 protected void _setByteOrder(int byteOrder) throws IOException { 260 super._setByteOrder(byteOrder); 261 if(bufferManager instanceof AbstractArrayBufferManager) { 262 ((AbstractArrayBufferManager) bufferManager).setByteOrder(getByteOrder()); 263 } 264 } 265 266 private int writeBytes(byte[] b, int off, int length) { 267 int len = Math.min(length, buf.length - fp); 268 if(len <= 0) { 269 return -1; 270 } 271 System.arraycopy(b, off, buf, fp, len); 272 return len; 273 } 274 275 282 public void write(int b) throws IOException { 283 if(fp < buf.length) { 284 buf[fp++] = (byte) b; 285 } 286 else { 287 nextArray(); 288 if(buf == null) { 289 throw new EOFException (); 290 } 291 buf[fp++] = (byte) b; 292 } 293 dirty = true; 294 } 295 296 299 public void close() { 300 if(dirty) { 301 bufferManager.setDirty(dataIndex); 302 dirty = false; 303 } 304 bufferManager.close(); 305 } 306 307 public int skip(int n) throws IOException { 308 return skipBytes(n); 309 } 310 311 protected int _read() throws EOFException { 312 int a = read(); 313 if(a < 0) { 314 throw new EOFException (); 315 } 316 return a; 317 } 318 } 319 | Popular Tags |