01: /*
02: * BinaryInputStream.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: BinaryInputStream.java,v 1.6 2003/11/15 11:03:31 beedlem Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: import java.io.BufferedInputStream;
25: import java.io.IOException;
26: import java.io.InputStream;
27:
28: public final class BinaryInputStream extends LispInputStream {
29: private final BufferedInputStream in;
30:
31: public BinaryInputStream(InputStream inputStream) {
32: in = new BufferedInputStream(inputStream);
33: }
34:
35: // read-byte stream &optional eof-error-p eof-value => byte
36: public LispObject readByte(boolean eofError, LispObject eofValue)
37: throws ConditionThrowable {
38: int n;
39: try {
40: n = in.read();
41: } catch (IOException e) {
42: throw new ConditionThrowable(new StreamError(e));
43: }
44: if (n < 0) {
45: if (eofError)
46: throw new ConditionThrowable(new EndOfFile());
47: else
48: return eofValue;
49: }
50: return new Fixnum(n);
51: }
52:
53: // Returns true if stream was open, otherwise implementation-dependent.
54: public LispObject close(LispObject abort) throws ConditionThrowable {
55: try {
56: in.close();
57: return T;
58: } catch (IOException e) {
59: throw new ConditionThrowable(new StreamError(e));
60: }
61: }
62:
63: public String toString() {
64: return unreadableString("STREAM [binary input]");
65: }
66: }
|