| 1 | /* |
|---|
| 2 | * SlimeOutputStream.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004-2005 Andras Simon, Peter Graves |
|---|
| 5 | * $Id: SlimeOutputStream.java 12513 2010-03-02 22:35:36Z 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 | setWriter(stringWriter = new StringWriter()); |
|---|
| 54 | this.f = f; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | @Override |
|---|
| 58 | public LispObject typeOf() |
|---|
| 59 | { |
|---|
| 60 | return Symbol.SLIME_OUTPUT_STREAM; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | @Override |
|---|
| 64 | public LispObject classOf() |
|---|
| 65 | { |
|---|
| 66 | return BuiltInClass.SLIME_OUTPUT_STREAM; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | @Override |
|---|
| 70 | public LispObject typep(LispObject type) |
|---|
| 71 | { |
|---|
| 72 | if (type == Symbol.SLIME_OUTPUT_STREAM) |
|---|
| 73 | return T; |
|---|
| 74 | if (type == Symbol.STRING_STREAM) |
|---|
| 75 | return T; |
|---|
| 76 | if (type == BuiltInClass.SLIME_OUTPUT_STREAM) |
|---|
| 77 | return T; |
|---|
| 78 | if (type == BuiltInClass.STRING_STREAM) |
|---|
| 79 | return T; |
|---|
| 80 | return super.typep(type); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | @Override |
|---|
| 84 | public void _writeChar(char c) |
|---|
| 85 | { |
|---|
| 86 | if (elementType == NIL) |
|---|
| 87 | writeError(); |
|---|
| 88 | super._writeChar(c); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | @Override |
|---|
| 92 | public void _writeChars(char[] chars, int start, int end) |
|---|
| 93 | |
|---|
| 94 | { |
|---|
| 95 | if (elementType == NIL) |
|---|
| 96 | writeError(); |
|---|
| 97 | super._writeChars(chars, start, end); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | @Override |
|---|
| 101 | public void _writeString(String s) |
|---|
| 102 | { |
|---|
| 103 | if (elementType == NIL) |
|---|
| 104 | writeError(); |
|---|
| 105 | super._writeString(s); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | @Override |
|---|
| 109 | public void _writeLine(String s) |
|---|
| 110 | { |
|---|
| 111 | if (elementType == NIL) |
|---|
| 112 | writeError(); |
|---|
| 113 | super._writeLine(s); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | private void writeError() |
|---|
| 117 | { |
|---|
| 118 | error(new TypeError("Attempt to write to a string output stream of element type NIL.")); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | @Override |
|---|
| 122 | protected long _getFilePosition() |
|---|
| 123 | { |
|---|
| 124 | if (elementType == NIL) |
|---|
| 125 | return 0; |
|---|
| 126 | return stringWriter.toString().length(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | @Override |
|---|
| 130 | public void _finishOutput() |
|---|
| 131 | { |
|---|
| 132 | super._finishOutput (); |
|---|
| 133 | if (stringWriter.getBuffer().length() > 0) { |
|---|
| 134 | String s = stringWriter.toString(); |
|---|
| 135 | stringWriter.getBuffer().setLength(0); |
|---|
| 136 | LispThread.currentThread().execute(f, new SimpleString(s)); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | @Override |
|---|
| 141 | public String toString() |
|---|
| 142 | { |
|---|
| 143 | return unreadableString("SLIME-OUTPUT-STREAM"); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | // ### %make-slime-output-stream |
|---|
| 147 | // %make-slime-output-stream function => stream |
|---|
| 148 | private static final Primitive MAKE_SLIME_OUTPUT_STREAM = |
|---|
| 149 | new Primitive("make-slime-output-stream", PACKAGE_EXT, true, "function") |
|---|
| 150 | { |
|---|
| 151 | @Override |
|---|
| 152 | public LispObject execute(LispObject arg) |
|---|
| 153 | { |
|---|
| 154 | final Function fun; |
|---|
| 155 | if (arg instanceof Symbol) |
|---|
| 156 | fun = (Function)arg.getSymbolFunction(); |
|---|
| 157 | else |
|---|
| 158 | fun = (Function)arg; |
|---|
| 159 | return new SlimeOutputStream(fun); |
|---|
| 160 | } |
|---|
| 161 | }; |
|---|
| 162 | } |
|---|