KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > BufferStream


1 /*
2  * BufferStream.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: BufferStream.java,v 1.1 2004/08/30 18:04:43 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.j;
23
24 import org.armedbear.lisp.ConditionThrowable;
25 import org.armedbear.lisp.LispObject;
26 import org.armedbear.lisp.Stream;
27 import org.armedbear.lisp.Symbol;
28
29 public final class BufferStream extends Stream
30 {
31     private final Buffer buffer;
32
33     public BufferStream(Buffer buf)
34     {
35         buffer = buf;
36         elementType = Symbol.CHARACTER;
37         isCharacterStream = true;
38         isOutputStream = true;
39     }
40
41     public Buffer getBuffer()
42     {
43         return buffer;
44     }
45
46     public LispObject typeOf()
47     {
48         return LispAPI.BUFFER_STREAM;
49     }
50
51 // // FIXME
52
// public LispClass classOf()
53
// {
54
// return BuiltInClass.STREAM;
55
// }
56

57     // FIXME
58
public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable
59     {
60         if (typeSpecifier == LispAPI.BUFFER_STREAM)
61             return T;
62         return super.typep(typeSpecifier);
63     }
64
65     public void _writeChar(char c) throws ConditionThrowable
66     {
67         try {
68             buffer.lockWrite();
69         }
70         catch (InterruptedException JavaDoc e) {
71             Log.error(e);
72             return;
73         }
74         try {
75             switch (c) {
76                 case '\r':
77                     break;
78                 case '\n': {
79                     buffer.appendLine("");
80                     buffer.modified();
81                     buffer.needsRenumbering(true);
82                     break;
83                 }
84                 default: {
85                     Line line = buffer.getLastLine();
86                     int offset = line.length();
87                     FastStringBuffer sb =
88                         new FastStringBuffer(line.getText());
89                     sb.append(c);
90                     line.setText(sb.toString());
91                     buffer.modified();
92                 }
93             }
94         }
95         finally {
96             buffer.unlockWrite();
97         }
98     }
99
100     public void _writeChars(char[] chars, int start, int end)
101         throws ConditionThrowable
102     {
103         _writeString(new String JavaDoc(chars, start, end - start));
104     }
105
106     public void _writeString(String JavaDoc s) throws ConditionThrowable
107     {
108         try {
109             buffer.lockWrite();
110         }
111         catch (InterruptedException JavaDoc e) {
112             Log.error(e);
113             return;
114         }
115         try {
116             buffer.append(s);
117             buffer.modified();
118             if (s.indexOf('\n') >= 0)
119                 buffer.needsRenumbering(true);
120         }
121         finally {
122             buffer.unlockWrite();
123         }
124     }
125
126     public void _writeLine(String JavaDoc s) throws ConditionThrowable
127     {
128         try {
129             buffer.lockWrite();
130         }
131         catch (InterruptedException JavaDoc e) {
132             Log.error(e);
133             return;
134         }
135         try {
136             buffer.append(s);
137             buffer.appendLine("");
138             buffer.modified();
139             buffer.needsRenumbering(true);
140         }
141         finally {
142             buffer.unlockWrite();
143         }
144     }
145
146     public void _finishOutput()
147     {
148         if (buffer.needsRenumbering())
149             buffer.renumber();
150         buffer.repaint();
151     }
152
153     public void _close()
154     {
155         _finishOutput();
156         setOpen(false);
157     }
158
159     public String JavaDoc toString()
160     {
161         return unreadableString("BUFFER-STREAM");
162     }
163 }
164
Popular Tags