001: /* Endian
002: *
003: * Created on September 12, 2006
004: *
005: * Copyright (C) 2006 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.io;
024:
025: import java.io.EOFException;
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: /**
030: * Reads integers stored in big or little endian streams.
031: *
032: * @author pjack
033: */
034: public class Endian {
035:
036: /**
037: * Static utility class.
038: */
039: private Endian() {
040: }
041:
042: /**
043: * Reads the next little-endian unsigned 16 bit integer from the
044: * given stream.
045: *
046: * @param input the input stream to read from
047: * @return the next 16-bit little-endian integer
048: * @throws IOException if an IO error occurs
049: */
050: public static char littleChar(InputStream input) throws IOException {
051: int lo = input.read();
052: if (lo < 0) {
053: throw new EOFException();
054: }
055: int hi = input.read();
056: if (hi < 0) {
057: throw new EOFException();
058: }
059: return (char) ((hi << 8) | lo);
060: }
061:
062: /**
063: * Reads the next little-endian signed 16-bit integer from the
064: * given stream.
065: *
066: * @param input the input stream to read from
067: * @return the next 16-bit little-endian integer
068: * @throws IOException if an IO error occurs
069: */
070: public static short littleShort(InputStream input)
071: throws IOException {
072: return (short) littleChar(input);
073: }
074:
075: /**
076: * Reads the next little-endian signed 32-bit integer from the
077: * given stream.
078: *
079: * @param input the input stream to read from
080: * @return the next 32-bit little-endian integer
081: * @throws IOException if an IO error occurs
082: */
083: public static int littleInt(InputStream input) throws IOException {
084: char lo = littleChar(input);
085: char hi = littleChar(input);
086: return (hi << 16) | lo;
087: }
088:
089: /**
090: * Reads the next big-endian unsigned 16 bit integer from the
091: * given stream.
092: *
093: * @param input the input stream to read from
094: * @return the next 16-bit big-endian integer
095: * @throws IOException if an IO error occurs
096: */
097: public static char bigChar(InputStream input) throws IOException {
098: int hi = input.read();
099: if (hi < 0) {
100: throw new EOFException();
101: }
102: int lo = input.read();
103: if (lo < 0) {
104: throw new EOFException();
105: }
106: return (char) ((hi << 8) | lo);
107: }
108:
109: /**
110: * Reads the next big-endian signed 32-bit integer from the
111: * given stream.
112: *
113: * @param input the input stream to read from
114: * @return the next 32-bit big-endian integer
115: * @throws IOException if an IO error occurs
116: */
117: public static int bigInt(InputStream input) throws IOException {
118: char hi = bigChar(input);
119: char lo = bigChar(input);
120: return (hi << 16) | lo;
121: }
122: }
|