01: /*
02: **********************************************************************
03: * Copyright (c) 2002-2006, International Business Machines
04: * Corporation and others. All Rights Reserved.
05: **********************************************************************
06: * Author: Alan Liu
07: * Created: November 5 2002
08: * Since: ICU 2.4
09: **********************************************************************
10: */
11: package com.ibm.icu.impl;
12:
13: import java.io.*;
14:
15: /**
16: * A DataInputStream that implements random-access seeking. For this
17: * to work, the size of the data stream must be known in advance, or
18: * the data must be supplied as a raw byte[] array.
19: *
20: * Seeking doesn't work directly on all streams. If a given stream
21: * doesn't support seeking, extract the bytes into a byte[] array and
22: * use the byte[] constructor.
23: */
24: class ICUBinaryStream extends DataInputStream {
25:
26: /**
27: * Construct a stream from the given stream and size.
28: * @param stream the stream of data
29: * @param size the number of bytes that should be made available
30: * for seeking. Bytes beyond this may be read, but seeking will
31: * not work for offset >= size.
32: */
33: public ICUBinaryStream(InputStream stream, int size) {
34: super (stream);
35: mark(size);
36: }
37:
38: /**
39: * Construct a stream from the given raw bytes.
40: */
41: public ICUBinaryStream(byte[] raw) {
42: this (new ByteArrayInputStream(raw), raw.length);
43: }
44:
45: /**
46: * Seek to the given offset. Offset is from the position of the
47: * stream passed to the constructor, or from the start of the
48: * byte[] array.
49: */
50: public void seek(int offset) throws IOException {
51: reset();
52: int actual = skipBytes(offset);
53: if (actual != offset) {
54: throw new IllegalStateException("Skip(" + offset
55: + ") only skipped " + actual + " bytes");
56: }
57: if (false)
58: System.out.println("(seek " + offset + ")");
59: }
60: }
|