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