source: branches/1.4.0/src/org/armedbear/lisp/StringOutputStream.java

Last change on this file was 14857, checked in by Mark Evenson, 9 years ago

[PATCH 5/5] Support FILE-POSITION on string streams.
From cb667c106187443ff2d00bace14f0ee0686fe2fd Mon Sep 17 00:00:00 2001
Adds a custom, seekable writer to be able to go back in the written
output for STRING-OUTPUT-STREAM - the input case is slightly less
complex.
---

build.xml | 1 +
src/org/armedbear/lisp/SeekableStringWriter.java | 140 +++++++++++++++++++++
src/org/armedbear/lisp/StringInputStream.java | 43 ++++++-
src/org/armedbear/lisp/StringOutputStream.java | 35 +++++-
test/lisp/abcl/misc-tests.lisp | 11 +-
.../armedbear/lisp/SeekableStringWriterTest.java | 19 +++
6 files changed, 242 insertions(+), 7 deletions(-)
create mode 100644 src/org/armedbear/lisp/SeekableStringWriter.java
create mode 100644 test/src/org/armedbear/lisp/SeekableStringWriterTest.java

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 * StringOutputStream.java
3 *
4 * Copyright (C) 2002-2004 Peter Graves
5 * $Id: StringOutputStream.java 14857 2016-09-04 07:01:02Z mevenson $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38import java.io.IOException;
39import java.io.StringWriter;
40
41public final class StringOutputStream extends Stream
42{
43    private final SeekableStringWriter stringWriter;
44
45    public StringOutputStream()
46    {
47        this(Symbol.CHARACTER);
48    }
49
50    StringOutputStream(LispObject elementType)
51    {
52        super(Symbol.STRING_OUTPUT_STREAM);
53        this.elementType = elementType;
54        this.eolStyle = EolStyle.RAW;
55        initAsCharacterOutputStream(stringWriter = new SeekableStringWriter());
56    }
57
58    @Override
59    public LispObject typeOf()
60    {
61        return Symbol.STRING_OUTPUT_STREAM;
62    }
63
64    @Override
65    public LispObject classOf()
66    {
67        return BuiltInClass.STRING_OUTPUT_STREAM;
68    }
69
70    @Override
71    public LispObject typep(LispObject type)
72    {
73        if (type == Symbol.STRING_OUTPUT_STREAM)
74            return T;
75        if (type == Symbol.STRING_STREAM)
76            return T;
77        if (type == BuiltInClass.STRING_OUTPUT_STREAM)
78            return T;
79        if (type == BuiltInClass.STRING_STREAM)
80            return T;
81        return super.typep(type);
82    }
83
84    @Override
85    protected long _getFilePosition()
86    {
87        if (elementType == NIL)
88            return 0;
89        return offset;
90    }
91
92    @Override
93    protected boolean _setFilePosition(LispObject arg) {
94        if (elementType == NIL)
95            return false;
96
97        try {
98            int offset;
99
100            if (arg == Keyword.START)
101                offset = 0;
102            else if (arg == Keyword.END)
103                offset = stringWriter.getBuffer().length();
104            else {
105                long n = Fixnum.getValue(arg);
106                offset = (int) n; // FIXME arg might be a bignum
107            }
108
109            stringWriter.seek(offset);
110
111            this.offset = offset;
112        }
113        catch (IllegalArgumentException e) {
114            error(new StreamError(this, e));
115        }
116
117        return true;
118    }
119
120    public LispObject getString()
121    {
122        if (elementType == NIL)
123            return new NilVector(0);
124        StringBuffer sb = stringWriter.getBuffer();
125        SimpleString s = new SimpleString(sb);
126        sb.setLength(0);
127        return s;
128    }
129
130    // ### %make-string-output-stream
131    // %make-string-output-stream element-type => string-stream
132    private static final Primitive MAKE_STRING_OUTPUT_STREAM =
133        new Primitive("%make-string-output-stream", PACKAGE_SYS, false,
134                       "element-type")
135    {
136        @Override
137        public LispObject execute(LispObject arg)
138        {
139            return new StringOutputStream(arg);
140        }
141    };
142
143    // ### get-output-stream-string
144    // get-output-stream-string string-output-stream => string
145    private static final Primitive GET_OUTPUT_STREAM_STRING =
146        new Primitive("get-output-stream-string", "string-output-stream")
147    {
148        @Override
149        public LispObject execute(LispObject arg)
150        {
151            if (arg instanceof StringOutputStream)
152                return ((StringOutputStream)arg).getString();
153            return type_error(this, Symbol.STRING_OUTPUT_STREAM);
154        }
155    };
156}
Note: See TracBrowser for help on using the repository browser.