001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.bytes;
028:
029: import java.io.RandomAccessFile;
030: import java.io.EOFException;
031: import java.io.IOException;
032: import java.nio.ByteOrder;
033:
034: /**
035: File stream.
036: @version 0.0.4
037: */
038: public final class FileInputStream implements IInputStream {
039: // <class>
040: // <dynamic>
041: // <fields>
042: private RandomAccessFile file;
043:
044: // </fields>
045:
046: // <constructors>
047: public FileInputStream(RandomAccessFile file) {
048: this .file = file;
049: }
050:
051: // </constructors>
052:
053: // <interface>
054: // <public>
055: // <IInputStream>
056: public ByteOrder getByteOrder() {
057: return ByteOrder.BIG_ENDIAN;
058: }
059:
060: public long getLength() {
061: try {
062: return file.length();
063: } catch (IOException e) {
064: throw new RuntimeException(e);
065: }
066: }
067:
068: public long getPosition() {
069: try {
070: return file.getFilePointer();
071: } catch (IOException e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: public int hashCode() {
077: return file.hashCode();
078: }
079:
080: public void read(byte[] data) throws EOFException {
081: try {
082: file.readFully(data);
083: } catch (EOFException e) {
084: throw e;
085: } catch (IOException e) {
086: throw new RuntimeException(e);
087: }
088: }
089:
090: public void read(byte[] data, int offset, int length)
091: throws EOFException {
092: try {
093: file.readFully(data, offset, length);
094: } catch (EOFException e) {
095: throw e;
096: } catch (IOException e) {
097: throw new RuntimeException(e);
098: }
099: }
100:
101: public byte readByte() throws EOFException {
102: try {
103: return file.readByte();
104: } catch (EOFException e) {
105: throw e;
106: } catch (IOException e) {
107: throw new RuntimeException(e);
108: }
109: }
110:
111: public int readInt() throws EOFException {
112: try {
113: return file.readInt();
114: } catch (EOFException e) {
115: throw e;
116: } catch (IOException e) {
117: throw new RuntimeException(e);
118: }
119: }
120:
121: public String readLine() throws EOFException {
122: try {
123: return file.readLine();
124: } catch (EOFException e) {
125: throw e;
126: } catch (IOException e) {
127: throw new RuntimeException(e);
128: }
129: }
130:
131: public short readShort() throws EOFException {
132: try {
133: return file.readShort();
134: } catch (EOFException e) {
135: throw e;
136: } catch (IOException e) {
137: throw new RuntimeException(e);
138: }
139: }
140:
141: public String readString(int length) throws EOFException {
142: byte[] data = new byte[length];
143: try {
144: file.readFully(data);
145:
146: return new String(data, "ISO-8859-1");
147: } catch (EOFException e) {
148: throw e;
149: } catch (Exception e) {
150: throw new RuntimeException(e);
151: }
152: }
153:
154: public int readUnsignedByte() throws EOFException {
155: try {
156: return file.readUnsignedByte();
157: } catch (EOFException e) {
158: throw e;
159: } catch (IOException e) {
160: throw new RuntimeException(e);
161: }
162: }
163:
164: public int readUnsignedShort() throws EOFException {
165: try {
166: return file.readUnsignedShort();
167: } catch (EOFException e) {
168: throw e;
169: } catch (IOException e) {
170: throw new RuntimeException(e);
171: }
172: }
173:
174: public void seek(long offset) {
175: try {
176: file.seek(offset);
177: } catch (IOException e) {
178: throw new RuntimeException(e);
179: }
180: }
181:
182: public void setByteOrder(ByteOrder value) {/* TODO */
183: }
184:
185: public void setPosition(long value) {
186: try {
187: file.seek(value);
188: } catch (IOException e) {
189: throw new RuntimeException(e);
190: }
191: }
192:
193: public void skip(long offset) {
194: try {
195: file.seek(file.getFilePointer() + offset);
196: } catch (IOException e) {
197: throw new RuntimeException(e);
198: }
199: }
200:
201: public byte[] toByteArray() {
202: byte[] data = null;
203: try {
204: file.seek(0);
205: data = new byte[(int) file.length()];
206: file.readFully(data);
207: } catch (IOException e) {
208: throw new RuntimeException(e);
209: }
210:
211: return data;
212: }
213: // </IInputStream>
214: // </public>
215: // </interface>
216: // </dynamic>
217: // </class>
218: }
|