KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > ICUBinaryStream


1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Author: Alan Liu
7 * Created: November 5 2002
8 * Since: ICU 2.4
9 **********************************************************************
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 JavaDoc("Skip(" + offset + ") only skipped " +
55                                        actual + " bytes");
56         }
57         if (false) System.out.println("(seek " + offset + ")");
58     }
59 }
60
Popular Tags