| 1 | /* | 
|---|
| 2 |  * SlimeOutputStream.java | 
|---|
| 3 |  * | 
|---|
| 4 |  * Copyright (C) 2004-2005 Andras Simon, Peter Graves | 
|---|
| 5 |  * $Id: SlimeOutputStream.java 14148 2012-09-02 18:11:30Z ehuelsmann $ | 
|---|
| 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 |  * As a special exception, the copyright holders of this library give you | 
|---|
| 22 |  * permission to link this library with independent modules to produce an | 
|---|
| 23 |  * executable, regardless of the license terms of these independent | 
|---|
| 24 |  * modules, and to copy and distribute the resulting executable under | 
|---|
| 25 |  * terms of your choice, provided that you also meet, for each linked | 
|---|
| 26 |  * independent module, the terms and conditions of the license of that | 
|---|
| 27 |  * module.  An independent module is a module which is not derived from | 
|---|
| 28 |  * or based on this library.  If you modify this library, you may extend | 
|---|
| 29 |  * this exception to your version of the library, but you are not | 
|---|
| 30 |  * obligated to do so.  If you do not wish to do so, delete this | 
|---|
| 31 |  * exception statement from your version. | 
|---|
| 32 |  */ | 
|---|
| 33 |  | 
|---|
| 34 | package org.armedbear.lisp; | 
|---|
| 35 |  | 
|---|
| 36 | import static org.armedbear.lisp.Lisp.*; | 
|---|
| 37 |  | 
|---|
| 38 | import java.io.StringWriter; | 
|---|
| 39 |  | 
|---|
| 40 | public final class SlimeOutputStream extends Stream | 
|---|
| 41 | { | 
|---|
| 42 |     private final StringWriter stringWriter; | 
|---|
| 43 |     final Function f; | 
|---|
| 44 |  | 
|---|
| 45 |     SlimeOutputStream(Function f) | 
|---|
| 46 |     { | 
|---|
| 47 |         super(Symbol.SLIME_OUTPUT_STREAM); | 
|---|
| 48 |         this.elementType = Symbol.CHARACTER; | 
|---|
| 49 |         isInputStream = false; | 
|---|
| 50 |         isOutputStream = true; | 
|---|
| 51 |         isCharacterStream = true; | 
|---|
| 52 |         isBinaryStream = false; | 
|---|
| 53 |         eolStyle = EolStyle.LF; | 
|---|
| 54 |         setWriter(stringWriter = new StringWriter()); | 
|---|
| 55 |         this.f = f; | 
|---|
| 56 |     } | 
|---|
| 57 |  | 
|---|
| 58 |     @Override | 
|---|
| 59 |     public LispObject typeOf() | 
|---|
| 60 |     { | 
|---|
| 61 |         return Symbol.SLIME_OUTPUT_STREAM; | 
|---|
| 62 |     } | 
|---|
| 63 |  | 
|---|
| 64 |     @Override | 
|---|
| 65 |     public LispObject classOf() | 
|---|
| 66 |     { | 
|---|
| 67 |         return BuiltInClass.SLIME_OUTPUT_STREAM; | 
|---|
| 68 |     } | 
|---|
| 69 |  | 
|---|
| 70 |     @Override | 
|---|
| 71 |     public LispObject typep(LispObject type) | 
|---|
| 72 |     { | 
|---|
| 73 |         if (type == Symbol.SLIME_OUTPUT_STREAM) | 
|---|
| 74 |             return T; | 
|---|
| 75 |         if (type == Symbol.STRING_STREAM) | 
|---|
| 76 |             return T; | 
|---|
| 77 |         if (type == BuiltInClass.SLIME_OUTPUT_STREAM) | 
|---|
| 78 |             return T; | 
|---|
| 79 |         if (type == BuiltInClass.STRING_STREAM) | 
|---|
| 80 |             return T; | 
|---|
| 81 |         return super.typep(type); | 
|---|
| 82 |     } | 
|---|
| 83 |  | 
|---|
| 84 |     @Override | 
|---|
| 85 |     public void _writeChar(char c) | 
|---|
| 86 |     { | 
|---|
| 87 |         if (elementType == NIL) | 
|---|
| 88 |             writeError(); | 
|---|
| 89 |         super._writeChar(c); | 
|---|
| 90 |     } | 
|---|
| 91 |  | 
|---|
| 92 |     @Override | 
|---|
| 93 |     public void _writeChars(char[] chars, int start, int end) | 
|---|
| 94 |  | 
|---|
| 95 |     { | 
|---|
| 96 |         if (elementType == NIL) | 
|---|
| 97 |             writeError(); | 
|---|
| 98 |         super._writeChars(chars, start, end); | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 |     @Override | 
|---|
| 102 |     public void _writeString(String s) | 
|---|
| 103 |     { | 
|---|
| 104 |         if (elementType == NIL) | 
|---|
| 105 |             writeError(); | 
|---|
| 106 |         super._writeString(s); | 
|---|
| 107 |     } | 
|---|
| 108 |  | 
|---|
| 109 |     @Override | 
|---|
| 110 |     public void _writeLine(String s) | 
|---|
| 111 |     { | 
|---|
| 112 |         if (elementType == NIL) | 
|---|
| 113 |             writeError(); | 
|---|
| 114 |         super._writeLine(s); | 
|---|
| 115 |     } | 
|---|
| 116 |  | 
|---|
| 117 |     private void writeError() | 
|---|
| 118 |     { | 
|---|
| 119 |         error(new TypeError("Attempt to write to a string output stream of element type NIL.")); | 
|---|
| 120 |     } | 
|---|
| 121 |  | 
|---|
| 122 |     @Override | 
|---|
| 123 |     protected long _getFilePosition() | 
|---|
| 124 |     { | 
|---|
| 125 |         if (elementType == NIL) | 
|---|
| 126 |             return 0; | 
|---|
| 127 |         return stringWriter.toString().length(); | 
|---|
| 128 |     } | 
|---|
| 129 |  | 
|---|
| 130 |     @Override | 
|---|
| 131 |     public void _finishOutput() | 
|---|
| 132 |     { | 
|---|
| 133 |         super._finishOutput (); | 
|---|
| 134 |         if (stringWriter.getBuffer().length() > 0) { | 
|---|
| 135 |             String s = stringWriter.toString(); | 
|---|
| 136 |             stringWriter.getBuffer().setLength(0); | 
|---|
| 137 |             LispThread.currentThread().execute(f, new SimpleString(s)); | 
|---|
| 138 |         } | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     // ### %make-slime-output-stream | 
|---|
| 142 |     // %make-slime-output-stream function => stream | 
|---|
| 143 |     private static final Primitive MAKE_SLIME_OUTPUT_STREAM = | 
|---|
| 144 |         new Primitive("make-slime-output-stream", PACKAGE_EXT, true, "function") | 
|---|
| 145 |     { | 
|---|
| 146 |         @Override | 
|---|
| 147 |         public LispObject execute(LispObject arg) | 
|---|
| 148 |         { | 
|---|
| 149 |             final Function fun; | 
|---|
| 150 |             if (arg instanceof Symbol) | 
|---|
| 151 |                 fun = (Function)arg.getSymbolFunction(); | 
|---|
| 152 |             else | 
|---|
| 153 |                 fun = (Function)arg; | 
|---|
| 154 |             return new SlimeOutputStream(fun); | 
|---|
| 155 |         } | 
|---|
| 156 |     }; | 
|---|
| 157 | } | 
|---|