KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > FillPointerOutputStream


1 /*
2  * FillPointerOutputStream.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: FillPointerOutputStream.java,v 1.10 2004/05/28 18:12:41 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 {
26     private ComplexString string;
27
28     private FillPointerOutputStream(ComplexString string)
29     {
30         elementType = Symbol.CHARACTER;
31         isOutputStream = true;
32         isInputStream = false;
33         isCharacterStream = true;
34         isBinaryStream = false;
35         this.string = string;
36         setWriter(new Writer());
37     }
38
39     // ### make-fill-pointer-output-stream
40
// make-fill-pointer-output-stream string => string-stream
41
private static final Primitive1 MAKE_FILL_POINTER_OUTPUT_STREAM =
42         new Primitive1("make-fill-pointer-output-stream", PACKAGE_SYS, false) {
43         public LispObject execute(LispObject arg) throws ConditionThrowable
44         {
45             if (arg instanceof ComplexString) {
46                 ComplexString string = (ComplexString) arg;
47                 if (string.getFillPointer() >= 0)
48                     return new FillPointerOutputStream(string);
49             }
50             return signal(new TypeError(arg.writeToString() +
51                                         " is not a string with a fill pointer."));
52         }
53     };
54
55     private class Writer extends java.io.Writer JavaDoc
56     {
57         public void write(char cbuf[], int off, int len)
58         {
59             int fp = string.getFillPointer();
60             if (fp >= 0) {
61                 final int capacity = string.capacity();
62                 final int limit = Math.min(cbuf.length, off + len);
63                 for (int i = off; i < limit && fp < capacity; i++) {
64                     try {
65                         string.setChar(fp, cbuf[i]);
66                     }
67                     catch (ConditionThrowable t) {
68                         // Shouldn't happen.
69
Debug.trace(t);
70                     }
71                     ++fp;
72                 }
73             }
74             string.setFillPointer(fp);
75         }
76
77         public void flush()
78         {
79         }
80
81         public void close()
82         {
83         }
84     }
85 }
86
Popular Tags