01: /*
02: * FillPointerOutputStream.java
03: *
04: * Copyright (C) 2003-2004 Peter Graves
05: * $Id: FillPointerOutputStream.java,v 1.10 2004/05/28 18:12:41 piso 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: public final class FillPointerOutputStream extends Stream {
25: private ComplexString string;
26:
27: private FillPointerOutputStream(ComplexString string) {
28: elementType = Symbol.CHARACTER;
29: isOutputStream = true;
30: isInputStream = false;
31: isCharacterStream = true;
32: isBinaryStream = false;
33: this .string = string;
34: setWriter(new Writer());
35: }
36:
37: // ### make-fill-pointer-output-stream
38: // make-fill-pointer-output-stream string => string-stream
39: private static final Primitive1 MAKE_FILL_POINTER_OUTPUT_STREAM = new Primitive1(
40: "make-fill-pointer-output-stream", PACKAGE_SYS, false) {
41: public LispObject execute(LispObject arg)
42: throws ConditionThrowable {
43: if (arg instanceof ComplexString) {
44: ComplexString string = (ComplexString) arg;
45: if (string.getFillPointer() >= 0)
46: return new FillPointerOutputStream(string);
47: }
48: return signal(new TypeError(arg.writeToString()
49: + " is not a string with a fill pointer."));
50: }
51: };
52:
53: private class Writer extends java.io.Writer {
54: public void write(char cbuf[], int off, int len) {
55: int fp = string.getFillPointer();
56: if (fp >= 0) {
57: final int capacity = string.capacity();
58: final int limit = Math.min(cbuf.length, off + len);
59: for (int i = off; i < limit && fp < capacity; i++) {
60: try {
61: string.setChar(fp, cbuf[i]);
62: } catch (ConditionThrowable t) {
63: // Shouldn't happen.
64: Debug.trace(t);
65: }
66: ++fp;
67: }
68: }
69: string.setFillPointer(fp);
70: }
71:
72: public void flush() {
73: }
74:
75: public void close() {
76: }
77: }
78: }
|