]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/ICUBinaryStream.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / ICUBinaryStream.java
1 /*\r
2 **********************************************************************\r
3 * Copyright (c) 2002-2010, 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.ByteArrayInputStream;\r
14 import java.io.DataInputStream;\r
15 import java.io.IOException;\r
16 import java.io.InputStream;\r
17 \r
18 /**\r
19  * A DataInputStream that implements random-access seeking.  For this\r
20  * to work, the size of the data stream must be known in advance, or\r
21  * the data must be supplied as a raw byte[] array.\r
22  *\r
23  * Seeking doesn't work directly on all streams.  If a given stream\r
24  * doesn't support seeking, extract the bytes into a byte[] array and\r
25  * use the byte[] constructor.\r
26  */\r
27 class ICUBinaryStream extends DataInputStream {\r
28 \r
29     /**\r
30      * Construct a stream from the given stream and size.\r
31      * @param stream the stream of data\r
32      * @param size the number of bytes that should be made available\r
33      * for seeking.  Bytes beyond this may be read, but seeking will\r
34      * not work for offset >= size.\r
35      */\r
36     public ICUBinaryStream(InputStream stream, int size) {\r
37         super(stream);\r
38         mark(size);\r
39     }\r
40 \r
41     /**\r
42      * Construct a stream from the given raw bytes.\r
43      */\r
44     public ICUBinaryStream(byte[] raw) {\r
45         this(new ByteArrayInputStream(raw), raw.length);\r
46     }\r
47 \r
48     /**\r
49      * Seek to the given offset.  Offset is from the position of the\r
50      * stream passed to the constructor, or from the start of the\r
51      * byte[] array.\r
52      */\r
53     public void seek(int offset) throws IOException {\r
54         reset();\r
55         int actual = skipBytes(offset);\r
56         if (actual != offset) {\r
57             throw new IllegalStateException("Skip(" + offset + ") only skipped " +\r
58                                        actual + " bytes");\r
59         }\r
60     }\r
61 }\r