Changeset 14857


Ignore:
Timestamp:
09/04/16 07:01:02 (5 years ago)
Author:
Mark Evenson
Message:

[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

Location:
trunk/abcl
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/build.xml

    r14846 r14857  
    947947        <arg value="org.armedbear.lisp.PathnameTest"/>
    948948        <arg value="org.armedbear.lisp.StreamTest"/>
     949        <arg value="org.armedbear.lisp.SeekableStringWriterTest"/>
    949950        <arg value="org.armedbear.lisp.UtilitiesTest"/>
    950951    <!-- currently hangs(!) the running process
  • trunk/abcl/src/org/armedbear/lisp/StringInputStream.java

    r13442 r14857  
    3636import static org.armedbear.lisp.Lisp.*;
    3737
     38import java.io.IOException;
    3839import java.io.StringReader;
    3940
     
    4243    private final StringReader stringReader;
    4344    private final int start;
     45    private final String subString;
    4446   
    4547    public StringInputStream(String s)
     
    6163
    6264        this.start = start;
    63        
    64         stringReader = new StringReader(s.substring(start, end));
     65
     66        subString = s.substring(start, end);
     67        stringReader = new StringReader(subString);
    6568        initAsCharacterInputStream(stringReader);
    6669    }
     
    9497    @Override
    9598    public int getOffset() {
    96         return start + super.getOffset();
     99        return start + offset;
     100    }
     101
     102    @Override
     103    protected long _getFilePosition() {
     104        return getOffset();
     105    }
     106
     107    @Override
     108    protected boolean _setFilePosition(LispObject arg) {
     109        try {
     110            int offset;
     111
     112            if (arg == Keyword.START)
     113                offset = 0;
     114            else if (arg == Keyword.END)
     115                offset = subString.length();
     116            else {
     117                long n = Fixnum.getValue(arg);
     118                if (n < 0 || n > subString.length())
     119                    error(new StreamError(this, "FILE-POSITION got out of bounds argument."));
     120                offset = (int) n; // FIXME arg might be a bignum
     121            }
     122
     123            stringReader.reset();
     124            stringReader.skip(offset);
     125            initAsCharacterInputStream(stringReader);
     126
     127            this.offset = offset;
     128        }
     129        catch (IOException e) {
     130            error(new StreamError(this, e));
     131        }
     132
     133        return true;
    97134    }
    98135   
  • trunk/abcl/src/org/armedbear/lisp/StringOutputStream.java

    r13442 r14857  
    3636import static org.armedbear.lisp.Lisp.*;
    3737
     38import java.io.IOException;
    3839import java.io.StringWriter;
    3940
    4041public final class StringOutputStream extends Stream
    4142{
    42     private final StringWriter stringWriter;
     43    private final SeekableStringWriter stringWriter;
    4344
    4445    public StringOutputStream()
     
    5253        this.elementType = elementType;
    5354        this.eolStyle = EolStyle.RAW;
    54         initAsCharacterOutputStream(stringWriter = new StringWriter());
     55        initAsCharacterOutputStream(stringWriter = new SeekableStringWriter());
    5556    }
    5657
     
    8687        if (elementType == NIL)
    8788            return 0;
    88         return stringWriter.getBuffer().length();
     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;
    89118    }
    90119
  • trunk/abcl/test/lisp/abcl/misc-tests.lisp

    r12935 r14857  
    110110                                 (values 42 2))))))
    111111  42 2)
     112
     113(deftest string-output-stream.seekable
     114    (string= "Goodbye, World! Something."
     115             (let ((stream (make-string-output-stream)))
     116               (write-string "Hello, World!   Something." stream)
     117               (file-position stream :start)
     118               (write-string "Goodbye, World!" stream)
     119               (get-output-stream-string stream)))
     120  T)
Note: See TracChangeset for help on using the changeset viewer.