1 32 package com.imagero.uio.io; 33 34 import com.imagero.uio.RandomAccess; 35 36 import java.io.IOException ; 37 38 42 public class TIFFStripInputStream extends RandomAccessInputStream { 43 44 int[] stripOffsets, stripByteCount; 45 46 int currentStrip = -1; 47 long stripLimit; 48 int markStrip; 49 50 public TIFFStripInputStream(RandomAccess ra, int[] stripByteCount, int[] stripOffsets) { 51 super(ra); 52 this.stripByteCount = stripByteCount; 53 this.stripOffsets = stripOffsets; 54 } 55 56 public TIFFStripInputStream(RandomAccess ra, long startPos, int[] stripByteCount, int[] stripOffsets) { 57 super(ra, startPos); 58 this.stripByteCount = stripByteCount; 59 this.stripOffsets = stripOffsets; 60 } 61 62 protected void checkPos() throws IOException { 63 if(pos > stripLimit || currentStrip == -1) { 64 if(currentStrip >= stripOffsets.length) { 66 throw new IOException (); 67 } 68 currentStrip++; 69 stripLimit = stripOffsets[currentStrip] + stripByteCount[currentStrip]; 70 pos = stripOffsets[currentStrip]; 71 72 } 86 super.checkPos(); 87 } 88 89 synchronized public int read(byte[] b, int off, int len) throws IOException { 90 return super.read(b, off, Math.min(len, (int)(stripLimit - pos))); 91 } 92 93 public void mark(int i) { 94 super.mark(i); 95 markStrip = currentStrip; 96 } 97 98 public void reset() throws IOException { 99 super.reset(); 100 currentStrip = markStrip; 101 } 102 103 public long skip(long l) throws IOException { 104 int lsi = stripOffsets.length - 1; 105 long limit = stripOffsets[lsi] + stripByteCount[lsi]; 106 long remaining = l; 107 while(remaining > 0) { 108 checkPos(); 109 long cs = Math.min(remaining, stripLimit - pos); 110 if(cs > limit - pos) { 111 cs = limit - pos; 112 remaining -= cs; 113 pos += cs; 114 break; 115 } 116 remaining -= cs; 117 pos += cs; 118 } 119 return l - remaining; 120 } 121 122 public int available() { 123 try { 124 return super.available(); 125 } 126 catch(IOException e) { 127 e.printStackTrace(); 128 } 129 return 0; 130 } 131 132 public long getPos() { 133 long res = 0; 134 for(int i = 0; i < currentStrip; i++) { 135 res += stripByteCount[i]; 136 } 137 res += stripByteCount[currentStrip] 138 - (stripByteCount[currentStrip] + stripOffsets[currentStrip] - pos); 139 return res; 140 } 141 } 142 | Popular Tags |