Fix CL:GET-OUTPUT-STREAM-STRING to reset underlying buffer

The contribution on supporting FILE-POSITION for string streams in
<http://abcl.org/trac/changeset/14857> did not reset the underlying
buffer on reads via CL:GET-OUTPUT-STREAM-STRING as required by ANSI.

Fixes <http://abcl.org/trac/ticket/439>
parent 72fcdd37
......@@ -36,6 +36,7 @@ package org.armedbear.lisp;
import static org.armedbear.lisp.Lisp.*;
import java.io.Writer;
import java.text.MessageFormat;
public final class SeekableStringWriter extends Writer {
private final StringBuffer stringBuffer;
......@@ -81,11 +82,15 @@ public final class SeekableStringWriter extends Writer {
@Override
public void write(int c) {
try {
if (offset == stringBuffer.length())
stringBuffer.append((char) c);
else
stringBuffer.setCharAt(offset, (char) c);
++offset;
} catch (IndexOutOfBoundsException e) {
error(new JavaException(e));
}
}
@Override
......@@ -137,4 +142,12 @@ public final class SeekableStringWriter extends Writer {
@Override
public void flush() {}
public String toStringAndClear() {
String result = stringBuffer.toString();
stringBuffer.delete(0, stringBuffer.length());
offset = 0;
return result;
}
}
......@@ -117,14 +117,25 @@ public final class StringOutputStream extends Stream
return true;
}
public LispObject getString()
public LispObject getString() {
if (elementType == NIL) {
return new NilVector(0);
}
StringBuffer sb = stringWriter.getBuffer();
SimpleString s = new SimpleString(sb);
sb.setLength(0);
return s;
}
public LispObject getSimpleStringAndClear()
{
if (elementType == NIL)
return new NilVector(0);
StringBuffer sb = stringWriter.getBuffer();
SimpleString s = new SimpleString(sb);
sb.setLength(0);
return s;
String contents = stringWriter.toStringAndClear();
return new SimpleString(contents);
}
// ### %make-string-output-stream
......@@ -148,9 +159,10 @@ public final class StringOutputStream extends Stream
@Override
public LispObject execute(LispObject arg)
{
if (arg instanceof StringOutputStream)
return ((StringOutputStream)arg).getString();
return type_error(this, Symbol.STRING_OUTPUT_STREAM);
if (arg instanceof StringOutputStream) {
return ((StringOutputStream)arg).getSimpleStringAndClear();
}
return type_error(this, Symbol.STRING_OUTPUT_STREAM);
}
};
}
......@@ -16,4 +16,16 @@ public class SeekableStringWriterTest
writer.append("meow");
assertEquals("meowd", writer.toString());
}
@Test
public void writeAndClear() {
SeekableStringWriter writer = new SeekableStringWriter();
String buf1 = "empus";
String buf2 = " fugit";
writer.append('t').append(buf1).append(buf2, 1, 7);
assertEquals("tempus fugit", writer.toString());
String result = writer.toStringAndClear();
assertEquals("tempus fugit", result);
assertEquals("", writer.toString());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment