[armedbear-cvs] r11414 - branches/open-external-format/src/org/armedbear/lisp
Erik Huelsmann
ehuelsmann at common-lisp.net
Thu Dec 4 19:19:10 UTC 2008
Author: ehuelsmann
Date: Thu Dec 4 19:19:09 2008
New Revision: 11414
Log:
Un-duplicate Stream and FileStream implementations.
Patch by: Hideo at Yokohama
Tweaked by: me
Modified:
branches/open-external-format/src/org/armedbear/lisp/FileStream.java
branches/open-external-format/src/org/armedbear/lisp/Stream.java
Modified: branches/open-external-format/src/org/armedbear/lisp/FileStream.java
==============================================================================
--- branches/open-external-format/src/org/armedbear/lisp/FileStream.java (original)
+++ branches/open-external-format/src/org/armedbear/lisp/FileStream.java Thu Dec 4 19:19:09 2008
@@ -49,10 +49,6 @@
private final RandomAccessCharacterFile racf;
private final Pathname pathname;
private final int bytesPerUnit;
- private InputStream inst;
- private OutputStream outst;
- private Reader reader;
- private Writer writer;
public FileStream(Pathname pathname, String namestring,
LispObject elementType, LispObject direction,
@@ -113,10 +109,10 @@
isCharacterStream = true;
bytesPerUnit = 1;
if (isInputStream) {
- reader = racf.getReader();
+ initAsCharacterInputStream(racf.getReader());
}
if (isOutputStream) {
- writer = racf.getWriter();
+ initAsCharacterOutputStream(racf.getWriter());
}
} else {
isBinaryStream = true;
@@ -129,10 +125,10 @@
}
bytesPerUnit = width / 8;
if (isInputStream) {
- inst = racf.getInputStream();
+ initAsBinaryInputStream(racf.getInputStream());
}
if (isOutputStream) {
- outst = racf.getOutputStream();
+ initAsBinaryOutputStream(racf.getOutputStream());
}
}
}
@@ -165,23 +161,6 @@
}
@Override
- public LispObject listen() throws ConditionThrowable
- {
- try {
- if (isInputStream) {
- return (racf.position() < racf.length()) ? T : NIL;
- } else {
- streamNotInputStream();
- }
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- // Not reached.
- return NIL;
- }
-
- @Override
public LispObject fileLength() throws ConditionThrowable
{
final long length;
@@ -209,44 +188,6 @@
return number(length / bytesPerUnit);
}
- // Returns -1 at end of file.
- @Override
- protected int _readChar() throws ConditionThrowable
- {
- try {
- int c = reader.read();
- if (eolStyle == EolStyle.CRLF) {
- if (c == '\r') {
- int c2 = reader.read();
- if (c2 == '\n') {
- ++lineNumber;
- return c2;
- } else {
- // '\r' was not followed by '\n'
- // we cannot depend on characters to contain 1 byte only
- // so we need to revert to the last known position.
- // The classical use case for unreadChar
- racf.unreadChar((char)c2);
- }
- }
- return c;
- } else if (c == eolChar) {
- ++lineNumber;
- return c;
- } else {
- return c;
- }
- }
- catch (NullPointerException e) {
- streamNotInputStream();
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- // Not reached.
- return -1;
- }
-
@Override
protected void _unreadChar(int n) throws ConditionThrowable
{
@@ -265,129 +206,6 @@
}
@Override
- public void _writeChar(char c) throws ConditionThrowable
- {
- try {
- if (c == '\n') {
- if (eolStyle == EolStyle.CRLF)
- writer.write('\r');
- writer.write(eolChar);
- charPos = 0;
- } else {
- writer.write(c);
- ++charPos;
- }
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- }
-
- @Override
- public void _writeChars(char[] chars, int start, int end)
- throws ConditionThrowable {
- _writeChars(chars, start, end, true);
- }
-
- public void _writeChars(char[] chars, int start, int end, boolean maintainCharPos)
- throws ConditionThrowable
- {
- try {
- if (eolStyle == EolStyle.LF) {
- /* we can do a little bit better in this special case */
- writer.write(chars, start, end);
- if (maintainCharPos) {
- int lastlfpos = -1;
- for (int i = start; i < end; i++) {
- if (chars[i] == '\n') {
- lastlfpos = i;
- }
- }
- if (lastlfpos == -1) {
- charPos += end - start;
- } else {
- charPos = end - lastlfpos;
- }
- }
- } else if (eolStyle == EolStyle.CRLF) {
- for (int i = start; i < end; i++) {
- char c = chars[i];
- if (c == '\n') {
- writer.write('\r');
- writer.write('\n');
- charPos = 0;
- } else {
- writer.write(c);
- ++charPos;
- }
- }
- } else {
- for (int i = start; i < end; i++) {
- char c = chars[i];
- if (c == '\n') {
- writer.write(eolChar);
- charPos = 0;
- } else {
- writer.write(c);
- ++charPos;
- }
- }
- }
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- }
-
- @Override
- public void _writeString(String s) throws ConditionThrowable
- {
- _writeChars(s.toCharArray(), 0, s.length(), true);
- }
-
- @Override
- public void _writeLine(String s) throws ConditionThrowable
- {
- _writeChars(s.toCharArray(), 0, s.length(), false);
- if (eolStyle == EolStyle.CRLF)
- _writeChar('\r');
- _writeChar(eolChar);
- charPos = 0;
- }
-
- // Reads an 8-bit byte.
- @Override
- public int _readByte() throws ConditionThrowable
- {
- try {
- return inst.read(); // Reads an 8-bit byte.
- }
- catch (NullPointerException e) {
- streamNotInputStream();
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- // Not reached.
- return -1;
- }
-
- // Writes an 8-bit byte.
- @Override
- public void _writeByte(int n) throws ConditionThrowable
- {
- try {
- outst.write(n); // Writes an 8-bit byte.
- }
- catch (NullPointerException e) {
- streamNotOutputStream();
- }
- catch (IOException e) {
- error(new StreamError(this, e));
- }
- }
-
- @Override
public void _clearInput() throws ConditionThrowable
{
try {
Modified: branches/open-external-format/src/org/armedbear/lisp/Stream.java
==============================================================================
--- branches/open-external-format/src/org/armedbear/lisp/Stream.java (original)
+++ branches/open-external-format/src/org/armedbear/lisp/Stream.java Thu Dec 4 19:19:09 2008
@@ -43,6 +43,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.PushbackReader;
+import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
@@ -66,7 +67,8 @@
private boolean open = true;
// Character input.
- private PushbackReader reader;
+ private Reader reader;
+ private PushbackReader pushbackReader;
protected int offset;
protected int lineNumber;
@@ -106,18 +108,32 @@
protected String encoding = null;
// Binary input.
- private BufferedInputStream in;
+ private InputStream in;
// Binary output.
- private BufferedOutputStream out;
+ private OutputStream out;
+
+ // end of line character sequence.
+ private char[] eolseq;
+
+ private void setEolSeq() {
+ if (eolStyle == EolStyle.CRLF) {
+ eolseq = new char[] { '\r', '\n' };
+ } else if (eolStyle == EolStyle.CR) {
+ eolseq = new char[] { '\r' };
+ } else {
+ eolseq = new char[] { '\n' };
+ }
+ }
protected Stream()
{
+ setEolSeq();
}
public Stream(InputStream inputStream, LispObject elementType)
{
- this(inputStream, elementType, null);
+ this(inputStream, elementType, null);
}
@@ -127,7 +143,6 @@
this.elementType = elementType;
if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR)
{
- isCharacterStream = true;
InputStreamReader inputStreamReader;
try
{
@@ -142,16 +157,17 @@
inputStreamReader =
new InputStreamReader(inputStream);
}
- reader = new PushbackReader(new BufferedReader(inputStreamReader),
- 2);
+ pushbackReader = new PushbackReader(new BufferedReader(inputStreamReader),
+ 2);
+ initAsCharacterInputStream(pushbackReader);
}
else
{
isBinaryStream = true;
- in = new BufferedInputStream(inputStream);
+ InputStream in = new BufferedInputStream(inputStream);
+ initAsBinaryInputStream(in);
}
- isInputStream = true;
- isOutputStream = false;
+ setEolSeq();
}
public Stream(InputStream inputStream, LispObject elementType, boolean interactive)
@@ -162,7 +178,7 @@
public Stream(OutputStream outputStream, LispObject elementType)
{
- this(outputStream, elementType, null);
+ this(outputStream, elementType, null);
}
// Output stream constructors.
@@ -171,7 +187,7 @@
this.elementType = elementType;
if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR)
{
- isCharacterStream = true;
+ Writer writer;
try
{
writer = (encoding == null) ?
@@ -183,14 +199,14 @@
Debug.trace(e);
writer = new OutputStreamWriter(outputStream);
}
+ initAsCharacterOutputStream(writer);
}
else
{
- isBinaryStream = true;
- out = new BufferedOutputStream(outputStream);
+ OutputStream out = new BufferedOutputStream(outputStream);
+ initAsBinaryOutputStream(out);
}
- isInputStream = false;
- isOutputStream = true;
+ setEolSeq();
}
public Stream(OutputStream outputStream, LispObject elementType,
@@ -200,6 +216,31 @@
setInteractive(interactive);
}
+ protected void initAsCharacterInputStream(Reader reader)
+ {
+ this.reader = reader;
+ isInputStream = true;
+ isCharacterStream = true;
+ }
+
+ protected void initAsBinaryInputStream(InputStream in) {
+ this.in = in;
+ isInputStream = true;
+ isBinaryStream = true;
+ }
+
+ protected void initAsCharacterOutputStream(Writer writer) {
+ this.writer = writer;
+ isOutputStream = true;
+ isCharacterStream = true;
+ }
+
+ protected void initAsBinaryOutputStream(OutputStream out) {
+ this.out = out;
+ isOutputStream = true;
+ isBinaryStream = true;
+ }
+
public boolean isInputStream() throws ConditionThrowable
{
return isInputStream;
@@ -254,7 +295,7 @@
eolStyle = platformEolStyle;
eolChar = (eolStyle == EolStyle.CR) ? '\r' : '\n';
externalFormat = format;
-
+ setEolSeq();
return;
}
@@ -1797,7 +1838,7 @@
{
try
{
- reader.unread(n);
+ pushbackReader.unread(n);
--offset;
if (n == eolChar)
--lineNumber;
@@ -1848,9 +1889,7 @@
try
{
if (c == '\n') {
- if (eolStyle == EolStyle.CRLF)
- writer.write('\r');
- writer.write(eolChar);
+ writer.write(eolseq);
writer.flush();
charPos = 0;
} else {
@@ -1898,19 +1937,19 @@
{
index = i;
break;
- }
- }
+ }
+ }
if (index < 0)
{
// No newline.
charPos += (end - start);
- }
+ }
else
{
charPos = end - (index + 1);
writer.flush();
- }
- }
+ }
+ }
catch (NullPointerException e)
{
if (writer == null)
@@ -1934,10 +1973,7 @@
{
try
{
- for (int i = 0; i < s.length(); i++)
- //###FIXME: the number of writes can be greatly reduced by
- // writing the space between newlines as chunks.
- _writeChar(s.charAt(i));
+ _writeChars(s.toCharArray(), 0, s.length());
}
catch (NullPointerException e)
{
More information about the armedbear-cvs
mailing list