Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
RandomAccessStream.java
1 package ij.io;
2 import java.io.*;
3 import java.util.Vector;
4 
5 
11 public final class RandomAccessStream extends InputStream {
12 
13  private static final int BLOCK_SIZE = 512;
14  private static final int BLOCK_MASK = 511;
15  private static final int BLOCK_SHIFT = 9;
16 
17  private InputStream src;
18  private RandomAccessFile ras;
19  private int pointer;
20  private Vector data;
21  private int length;
22  private boolean foundEOS;
23 
26  public RandomAccessStream(InputStream inputstream) {
27  pointer = 0;
28  data = new Vector();
29  length = 0;
30  foundEOS = false;
31  src = inputstream;
32  }
33 
35  public RandomAccessStream(RandomAccessFile ras) {
36  this.ras = ras;
37  }
38 
39  public int getFilePointer() throws IOException {
40  if (ras!=null)
41  return (int)ras.getFilePointer();
42  else
43  return pointer;
44  }
45 
46  public int read() throws IOException {
47  if (ras!=null)
48  return ras.read();
49  int l = pointer + 1;
50  int l1 = readUntil(l);
51  if(l1 >= l) {
52  byte abyte0[] = (byte[])data.elementAt((int)(pointer>>BLOCK_SHIFT));
53  return abyte0[(int)(pointer++ & BLOCK_MASK)] & 0xff;
54  } else
55  return -1;
56  }
57 
58  public int read(byte[] bytes, int off, int len) throws IOException {
59  if(bytes == null)
60  throw new NullPointerException();
61  if (ras!=null)
62  return ras.read(bytes, off, len);
63  if(off<0 || len<0 || off+len>bytes.length)
64  throw new IndexOutOfBoundsException();
65  if(len == 0)
66  return 0;
67  int l = readUntil(pointer+len);
68  if(l<=pointer)
69  return -1;
70  else {
71  byte abyte1[] = (byte[])data.elementAt((int)(pointer >> BLOCK_SHIFT));
72  int k = Math.min(len, BLOCK_SIZE - (int)(pointer & BLOCK_MASK));
73  System.arraycopy(abyte1, (int)(pointer & BLOCK_MASK), bytes, off, k);
74  pointer += k;
75  return k;
76  }
77  }
78 
79  public final void readFully(byte[] bytes) throws IOException {
80  //if (ij.IJ.debugMode) ij.IJ.log("readFully: "+bytes.length);
81  int read = 0;
82  int len = bytes.length;
83  do {
84  int l = read(bytes, read, len - read);
85  if(l < 0) break;
86  read += l;
87  } while (read<len);
88  }
89 
90  private int readUntil(int l) throws IOException {
91  if(l<length)
92  return l;
93  if(foundEOS)
94  return length;
95  int i = (int)(l >> BLOCK_SHIFT);
96  int j = length >> BLOCK_SHIFT;
97  for(int k = j; k <= i; k++) {
98  byte abyte0[] = new byte[BLOCK_SIZE];
99  data.addElement(abyte0);
100  int i1 = BLOCK_SIZE;
101  int j1 = 0;
102  while(i1 > 0) {
103  int k1 = src.read(abyte0, j1, i1);
104  if(k1 == -1) {
105  foundEOS = true;
106  return length;
107  }
108  j1 += k1;
109  i1 -= k1;
110  length += k1;
111  }
112 
113  }
114 
115  return length;
116  }
117 
118  public void seek(int loc) throws IOException {
119  //if (ij.IJ.debugMode) ij.IJ.log("seek: "+loc);
120  if (ras!=null)
121  {ras.seek(loc); return;}
122  if(loc < 0)
123  pointer = 0;
124  else
125  pointer = loc;
126  }
127 
128  public final int readInt() throws IOException {
129  int i = read();
130  int j = read();
131  int k = read();
132  int l = read();
133  if((i | j | k | l) < 0)
134  throw new EOFException();
135  else
136  return (i << 24) + (j << 16) + (k << 8) + l;
137  }
138 
139  public final long readLong() throws IOException {
140  return ((long)readInt() << 32) + ((long)readInt() & 0xffffffffL);
141  }
142 
143  public final double readDouble() throws IOException {
144  return Double.longBitsToDouble(readLong());
145  }
146 
147  public final short readShort() throws IOException {
148  int i = read();
149  int j = read();
150  if((i | j) < 0)
151  throw new EOFException();
152  else
153  return (short)((i << 8) + j);
154  }
155 
156  public final float readFloat() throws IOException {
157  return Float.intBitsToFloat(readInt());
158  }
159 
160  public void close() throws IOException {
161  //if (ij.IJ.debugMode) ij.IJ.log("close: "+(data!=null?""+data.size():""));
162  if (ras!=null)
163  ras.close();
164  else {
165  data.removeAllElements();
166  src.close();
167  }
168  }
169 
170 
171 }