KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > Endian


1 /* Endian
2 *
3 * Created on September 12, 2006
4 *
5 * Copyright (C) 2006 Internet Archive.
6 *
7 * This file is part of the Heritrix web crawler (crawler.archive.org).
8 *
9 * Heritrix is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * any later version.
13 *
14 * Heritrix is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with Heritrix; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.archive.io;
24
25
26 import java.io.EOFException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30
31 /**
32  * Reads integers stored in big or little endian streams.
33  *
34  * @author pjack
35  */

36 public class Endian {
37
38
39     /**
40      * Static utility class.
41      */

42     private Endian() {
43     }
44     
45
46     /**
47      * Reads the next little-endian unsigned 16 bit integer from the
48      * given stream.
49      *
50      * @param input the input stream to read from
51      * @return the next 16-bit little-endian integer
52      * @throws IOException if an IO error occurs
53      */

54     public static char littleChar(InputStream JavaDoc input) throws IOException JavaDoc {
55         int lo = input.read();
56         if (lo < 0) {
57             throw new EOFException JavaDoc();
58         }
59         int hi = input.read();
60         if (hi < 0) {
61             throw new EOFException JavaDoc();
62         }
63         return (char)((hi << 8) | lo);
64     }
65     
66     
67     /**
68      * Reads the next little-endian signed 16-bit integer from the
69      * given stream.
70      *
71      * @param input the input stream to read from
72      * @return the next 16-bit little-endian integer
73      * @throws IOException if an IO error occurs
74      */

75     public static short littleShort(InputStream JavaDoc input) throws IOException JavaDoc {
76         return (short)littleChar(input);
77     }
78
79
80     /**
81      * Reads the next little-endian signed 32-bit integer from the
82      * given stream.
83      *
84      * @param input the input stream to read from
85      * @return the next 32-bit little-endian integer
86      * @throws IOException if an IO error occurs
87      */

88     public static int littleInt(InputStream JavaDoc input) throws IOException JavaDoc {
89         char lo = littleChar(input);
90         char hi = littleChar(input);
91         return (hi << 16) | lo;
92     }
93
94     
95     /**
96      * Reads the next big-endian unsigned 16 bit integer from the
97      * given stream.
98      *
99      * @param input the input stream to read from
100      * @return the next 16-bit big-endian integer
101      * @throws IOException if an IO error occurs
102      */

103     public static char bigChar(InputStream JavaDoc input) throws IOException JavaDoc {
104         int hi = input.read();
105         if (hi < 0) {
106             throw new EOFException JavaDoc();
107         }
108         int lo = input.read();
109         if (lo < 0) {
110             throw new EOFException JavaDoc();
111         }
112         return (char)((hi << 8) | lo);
113     }
114
115
116     /**
117      * Reads the next big-endian signed 32-bit integer from the
118      * given stream.
119      *
120      * @param input the input stream to read from
121      * @return the next 32-bit big-endian integer
122      * @throws IOException if an IO error occurs
123      */

124     public static int bigInt(InputStream JavaDoc input) throws IOException JavaDoc {
125         char hi = bigChar(input);
126         char lo = bigChar(input);
127         return (hi << 16) | lo;
128     }
129 }
130
Popular Tags