1 32 package com.imagero.uio.io; 33 34 import com.imagero.uio.RandomAccess; 35 36 import java.io.IOException ; 37 import java.io.OutputStream ; 38 39 44 public class RandomAccessOutputStream extends OutputStream { 45 46 protected RandomAccess ra; 47 protected long pos; 48 49 public RandomAccessOutputStream(RandomAccess ra) { 50 this(ra, 0L); 51 } 52 53 public RandomAccessOutputStream(RandomAccess ra, long startPos) { 54 this.ra = ra; 55 this.pos = startPos; 56 } 57 58 protected void checkPos() throws IOException { 59 long fp = ra.getFilePointer(); 60 if (fp != pos) { 61 ra.seek(pos); 62 } 63 } 64 65 public void write(int b) throws IOException { 66 checkPos(); 67 writeImpl(b); 68 pos++; 69 } 70 71 private void writeImpl(int b) throws IOException { 72 ra.write(b); 73 } 74 75 public void write(byte b[]) throws IOException { 76 write(b, 0, b.length); 77 } 78 79 public void write(byte b[], int off, int len) throws IOException { 80 checkPos(); 81 ra.write(b, off, len); 82 pos += len; 83 } 84 85 public void close() throws IOException { 86 ra = null; 87 } 88 } 89 | Popular Tags |