| 1 | /* |
| 2 | * Copyright (C) 2007 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * |
| 14 | * Author: Steve Ratcliffe |
| 15 | * Create date: 23-Sep-2007 |
| 16 | */ |
| 17 | package uk.me.parabola.io; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | |
| 22 | /** |
| 23 | * An input stream used to make reading little endian integers and strings from |
| 24 | * the tdb file. |
| 25 | * |
| 26 | * @author Steve Ratcliffe |
| 27 | */ |
| 28 | public class StructuredInputStream extends InputStream { |
| 29 | private final InputStream in; |
| 30 | private boolean eof; |
| 31 | |
| 32 | public StructuredInputStream(InputStream in) { |
| 33 | this.in = in; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Implementation of read that delegates to the underlying stream. It however |
| 38 | * also keeps track of the end of file status. |
| 39 | * |
| 40 | * @return The next byte, or -1 on eof. |
| 41 | * @throws IOException For problems reading. |
| 42 | */ |
| 43 | public int read() throws IOException { |
| 44 | int r = in.read(); |
| 45 | if (r == -1) |
| 46 | eof = true; |
| 47 | return r; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Read a 2 byte little endian integer. |
| 52 | * |
| 53 | * @return The integer. |
| 54 | * @throws IOException If the stream could not be read. |
| 55 | */ |
| 56 | public int read2() throws IOException { |
| 57 | int a = read() & 0xff; |
| 58 | int b = read() & 0xff; |
| 59 | |
| 60 | if (isEof()) |
| 61 | throw new EndOfFileException(); |
| 62 | |
| 63 | return (b << 8) + a; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Read a 4 byte integer quantity. As always this is little endian. |
| 68 | * |
| 69 | * @return The integer. |
| 70 | * @throws IOException If the stream could not be read. |
| 71 | */ |
| 72 | public int read4() throws IOException { |
| 73 | int a = read() & 0xff; |
| 74 | int b = read() & 0xff; |
| 75 | int c = read() & 0xff; |
| 76 | int d = read() & 0xff; |
| 77 | |
| 78 | return (d << 24) | (c << 16) | (b << 8) | a; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Read a nul terminated string from the input stream. |
| 83 | * |
| 84 | * @return A string, without the null terminator. |
| 85 | * @throws IOException If the stream cannot be read. |
| 86 | */ |
| 87 | public String readString() throws IOException { |
| 88 | StringBuffer name = new StringBuffer(); |
| 89 | int b; |
| 90 | while ((b = read()) != '\0' && b != -1) { |
| 91 | name.append((char) (b & 0xff)); |
| 92 | } |
| 93 | return name.toString(); |
| 94 | } |
| 95 | |
| 96 | boolean isEof() { |
| 97 | return eof; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Test if we are at the end of the file by marking the position and trying |
| 102 | * to read the next byte. If not at the end then the stream position is |
| 103 | * reset and all is as before. |
| 104 | * |
| 105 | * @return True if we are at the end of the stream. |
| 106 | */ |
| 107 | public boolean testEof() { |
| 108 | assert in.markSupported(); |
| 109 | in.mark(1); |
| 110 | try { |
| 111 | int b = in.read(); |
| 112 | if (b == -1) { |
| 113 | eof = true; |
| 114 | } else { |
| 115 | in.reset(); |
| 116 | } |
| 117 | return isEof(); |
| 118 | } catch (IOException e) { |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } |