01: /*
02: * FillPointerOutputStream.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: FillPointerOutputStream.java,v 1.6 2003/11/15 11:03:30 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: public final class FillPointerOutputStream extends
25: CharacterOutputStream {
26: private LispString string;
27:
28: private FillPointerOutputStream(LispString string) {
29: this .string = string;
30: setWriter(new Writer());
31: }
32:
33: // ### make-fill-pointer-output-stream
34: // make-fill-pointer-output-stream string => string-stream
35: private static final Primitive1 MAKE_FILL_POINTER_OUTPUT_STREAM = new Primitive1(
36: "make-fill-pointer-output-stream", PACKAGE_SYS, false) {
37: public LispObject execute(LispObject arg)
38: throws ConditionThrowable {
39: LispString string = checkString(arg);
40: if (string.getFillPointer() < 0)
41: throw new ConditionThrowable(new TypeError(arg,
42: "string with a fill pointer"));
43: return new FillPointerOutputStream(string);
44: }
45: };
46:
47: private class Writer extends java.io.Writer {
48: public void write(char cbuf[], int off, int len) {
49: int fp = string.getFillPointer();
50: if (fp >= 0) {
51: final int capacity = string.capacity();
52: final int limit = Math.min(cbuf.length, off + len);
53: for (int i = off; i < limit && fp < capacity; i++) {
54: string.set(fp, cbuf[i]);
55: ++fp;
56: }
57: }
58: string.setFillPointer(fp);
59: }
60:
61: public void flush() {
62: }
63:
64: public void close() {
65: }
66: }
67: }
|