Changeset 11414


Ignore:
Timestamp:
12/04/08 19:19:09 (15 years ago)
Author:
ehuelsmann
Message:

Un-duplicate Stream and FileStream? implementations.

Patch by: Hideo at Yokohama
Tweaked by: me

Location:
branches/open-external-format/src/org/armedbear/lisp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/open-external-format/src/org/armedbear/lisp/FileStream.java

    r11406 r11414  
    5050    private final Pathname pathname;
    5151    private final int bytesPerUnit;
    52     private InputStream inst;
    53     private OutputStream outst;
    54     private Reader reader;
    55     private Writer writer;
    5652
    5753    public FileStream(Pathname pathname, String namestring,
     
    114110            bytesPerUnit = 1;
    115111      if (isInputStream) {
    116     reader = racf.getReader();
     112    initAsCharacterInputStream(racf.getReader());
    117113      }
    118114      if (isOutputStream) {
    119     writer = racf.getWriter();
     115    initAsCharacterOutputStream(racf.getWriter());
    120116      }
    121117        } else {
     
    130126            bytesPerUnit = width / 8;
    131127      if (isInputStream) {
    132     inst = racf.getInputStream();
     128    initAsBinaryInputStream(racf.getInputStream());
    133129      }
    134130      if (isOutputStream) {
    135     outst = racf.getOutputStream();
     131    initAsBinaryOutputStream(racf.getOutputStream());
    136132      }
    137133        }
     
    163159    {
    164160        return pathname;
    165     }
    166 
    167     @Override
    168     public LispObject listen() throws ConditionThrowable
    169     {
    170         try {
    171       if (isInputStream) {
    172     return (racf.position() < racf.length()) ? T : NIL;
    173       } else {
    174     streamNotInputStream();
    175       }
    176         }
    177   catch (IOException e) {
    178             error(new StreamError(this, e));
    179         }
    180         // Not reached.
    181         return NIL;
    182161    }
    183162
     
    210189    }
    211190
    212     // Returns -1 at end of file.
    213     @Override
    214     protected int _readChar() throws ConditionThrowable
    215     {
    216         try {
    217             int c = reader.read();
    218             if (eolStyle == EolStyle.CRLF) {
    219                 if (c == '\r') {
    220                     int c2 = reader.read();
    221                     if (c2 == '\n') {
    222                         ++lineNumber;
    223                         return c2;
    224                     } else {
    225       // '\r' was not followed by '\n'
    226       // we cannot depend on characters to contain 1 byte only
    227       // so we need to revert to the last known position.
    228       // The classical use case for unreadChar
    229       racf.unreadChar((char)c2);
    230         }
    231                 }
    232                 return c;
    233             } else if (c == eolChar) {
    234                 ++lineNumber;
    235                 return c;
    236             } else {
    237     return c;
    238       }
    239         }
    240         catch (NullPointerException e) {
    241             streamNotInputStream();
    242         }
    243         catch (IOException e) {
    244             error(new StreamError(this, e));
    245         }
    246         // Not reached.
    247         return -1;
    248     }
    249 
    250191    @Override
    251192    protected void _unreadChar(int n) throws ConditionThrowable
     
    263204    {
    264205        return true;
    265     }
    266 
    267     @Override
    268     public void _writeChar(char c) throws ConditionThrowable
    269     {
    270         try {
    271             if (c == '\n') {
    272                 if (eolStyle == EolStyle.CRLF)
    273                     writer.write('\r');
    274                 writer.write(eolChar);
    275                 charPos = 0;
    276             } else {
    277                 writer.write(c);
    278                 ++charPos;
    279             }
    280         }
    281         catch (IOException e) {
    282             error(new StreamError(this, e));
    283         }
    284     }
    285 
    286     @Override
    287     public void _writeChars(char[] chars, int start, int end)
    288         throws ConditionThrowable {
    289   _writeChars(chars, start, end, true);
    290     }
    291 
    292     public void _writeChars(char[] chars, int start, int end, boolean maintainCharPos)
    293         throws ConditionThrowable
    294     {
    295         try {
    296       if (eolStyle == EolStyle.LF) {
    297     /* we can do a little bit better in this special case */
    298     writer.write(chars, start, end);
    299     if (maintainCharPos) {
    300         int lastlfpos = -1;
    301         for (int i = start; i < end; i++) {
    302       if (chars[i] == '\n') {
    303           lastlfpos = i;
    304       }
    305         }
    306         if (lastlfpos == -1) {
    307       charPos += end - start;
    308         } else {
    309       charPos = end - lastlfpos;
    310         }
    311     }
    312       } else if (eolStyle == EolStyle.CRLF) {
    313                 for (int i = start; i < end; i++) {
    314                     char c = chars[i];
    315                     if (c == '\n') {
    316                         writer.write('\r');
    317                         writer.write('\n');
    318                         charPos = 0;
    319                     } else {
    320                         writer.write(c);
    321                         ++charPos;
    322                     }
    323                 }
    324             } else {
    325                 for (int i = start; i < end; i++) {
    326                     char c = chars[i];
    327                     if (c == '\n') {
    328                         writer.write(eolChar);
    329                         charPos = 0;
    330                     } else {
    331                         writer.write(c);
    332                         ++charPos;
    333                     }
    334                 }
    335             }
    336         }
    337         catch (IOException e) {
    338             error(new StreamError(this, e));
    339         }
    340     }
    341 
    342     @Override
    343     public void _writeString(String s) throws ConditionThrowable
    344     {
    345         _writeChars(s.toCharArray(), 0, s.length(), true);
    346     }
    347 
    348     @Override
    349     public void _writeLine(String s) throws ConditionThrowable
    350     {
    351   _writeChars(s.toCharArray(), 0, s.length(), false);
    352         if (eolStyle == EolStyle.CRLF)
    353             _writeChar('\r');
    354         _writeChar(eolChar);
    355         charPos = 0;
    356     }
    357 
    358     // Reads an 8-bit byte.
    359     @Override
    360     public int _readByte() throws ConditionThrowable
    361     {
    362         try {
    363             return inst.read(); // Reads an 8-bit byte.
    364         }
    365         catch (NullPointerException e) {
    366             streamNotInputStream();
    367         }
    368         catch (IOException e) {
    369             error(new StreamError(this, e));
    370         }
    371         // Not reached.
    372         return -1;
    373     }
    374 
    375     // Writes an 8-bit byte.
    376     @Override
    377     public void _writeByte(int n) throws ConditionThrowable
    378     {
    379         try {
    380             outst.write(n); // Writes an 8-bit byte.
    381         }
    382         catch (NullPointerException e) {
    383             streamNotOutputStream();
    384         }
    385         catch (IOException e) {
    386             error(new StreamError(this, e));
    387         }
    388206    }
    389207
  • branches/open-external-format/src/org/armedbear/lisp/Stream.java

    r11411 r11414  
    4444import java.io.PrintWriter;
    4545import java.io.PushbackReader;
     46import java.io.Reader;
    4647import java.io.StringWriter;
    4748import java.io.Writer;
     
    6768
    6869  // Character input.
    69   private PushbackReader reader;
     70  private Reader reader;
     71  private PushbackReader pushbackReader;
    7072  protected int offset;
    7173  protected int lineNumber;
     
    107109 
    108110  // Binary input.
    109   private BufferedInputStream in;
     111  private InputStream in;
    110112
    111113  // Binary output.
    112   private BufferedOutputStream out;
     114  private OutputStream out;
     115
     116  // end of line character sequence.
     117  private char[] eolseq;
     118 
     119  private void setEolSeq() {
     120    if (eolStyle == EolStyle.CRLF) {
     121      eolseq = new char[] { '\r', '\n' };
     122    } else if (eolStyle == EolStyle.CR) {
     123      eolseq = new char[] { '\r' };
     124    } else {
     125      eolseq = new char[] { '\n' };
     126    }
     127  }
    113128
    114129  protected Stream()
    115130  {
     131    setEolSeq();
    116132  }
    117133
    118134  public Stream(InputStream inputStream, LispObject elementType)
    119135    {
    120   this(inputStream, elementType, null);
     136      this(inputStream, elementType, null);
    121137    }
    122138
     
    128144    if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR)
    129145      {
    130         isCharacterStream = true;
    131146        InputStreamReader inputStreamReader;
    132147        try
     
    143158              new InputStreamReader(inputStream);
    144159          }
    145         reader = new PushbackReader(new BufferedReader(inputStreamReader),
    146                                     2);
     160        pushbackReader = new PushbackReader(new BufferedReader(inputStreamReader),
     161              2);
     162  initAsCharacterInputStream(pushbackReader);
    147163      }
    148164    else
    149165      {
    150166        isBinaryStream = true;
    151         in = new BufferedInputStream(inputStream);
    152       }
    153     isInputStream = true;
    154     isOutputStream = false;
     167        InputStream in = new BufferedInputStream(inputStream);
     168  initAsBinaryInputStream(in);
     169      }
     170    setEolSeq();
    155171  }
    156172
     
    163179  public Stream(OutputStream outputStream, LispObject elementType)
    164180    {
    165   this(outputStream, elementType, null);
     181      this(outputStream, elementType, null);
    166182    }
    167183   
     
    172188    if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR)
    173189      {
    174         isCharacterStream = true;
     190  Writer writer;
    175191        try
    176192          {
     
    184200            writer = new OutputStreamWriter(outputStream);
    185201          }
     202  initAsCharacterOutputStream(writer);
    186203      }
    187204    else
    188205      {
    189         isBinaryStream = true;
    190         out = new BufferedOutputStream(outputStream);
    191       }
    192     isInputStream = false;
    193     isOutputStream = true;
     206        OutputStream out = new BufferedOutputStream(outputStream);
     207  initAsBinaryOutputStream(out);
     208      }
     209    setEolSeq();
    194210  }
    195211
     
    199215    this(outputStream, elementType);
    200216    setInteractive(interactive);
     217  }
     218
     219  protected void initAsCharacterInputStream(Reader reader)
     220  {
     221    this.reader = reader;
     222    isInputStream = true;
     223    isCharacterStream = true;
     224  }
     225
     226  protected void initAsBinaryInputStream(InputStream in) {
     227    this.in = in;
     228    isInputStream = true;
     229    isBinaryStream = true;
     230  }
     231
     232  protected void initAsCharacterOutputStream(Writer writer) {
     233    this.writer = writer;
     234    isOutputStream = true;
     235    isCharacterStream = true;
     236  }
     237
     238  protected void initAsBinaryOutputStream(OutputStream out) {
     239    this.out = out;
     240    isOutputStream = true;
     241    isBinaryStream = true;
    201242  }
    202243
     
    255296      eolChar = (eolStyle == EolStyle.CR) ? '\r' : '\n';
    256297      externalFormat = format;
    257      
     298      setEolSeq();
    258299      return;
    259300    }
     
    17981839    try
    17991840      {
    1800         reader.unread(n);
     1841        pushbackReader.unread(n);
    18011842        --offset;
    18021843        if (n == eolChar)
     
    18491890      {
    18501891        if (c == '\n') {
    1851           if (eolStyle == EolStyle.CRLF)
    1852               writer.write('\r');
    1853           writer.write(eolChar);
     1892    writer.write(eolseq);
    18541893          writer.flush();
    18551894          charPos = 0;
     
    18991938                index = i;
    19001939                break;
    1901               }
    1902           }
     1940    }
     1941  }
    19031942        if (index < 0)
    19041943          {
    19051944            // No newline.
    19061945            charPos += (end - start);
    1907           }
     1946        }
    19081947        else
    19091948          {
    19101949            charPos = end - (index + 1);
    19111950            writer.flush();
    1912           }
    1913       }
     1951      }
     1952    }
    19141953    catch (NullPointerException e)
    19151954      {
     
    19351974    try
    19361975      {
    1937         for (int i = 0; i < s.length(); i++)
    1938           //###FIXME: the number of writes can be greatly reduced by
    1939           // writing the space between newlines as chunks.
    1940           _writeChar(s.charAt(i));
     1976  _writeChars(s.toCharArray(), 0, s.length());
    19411977      }
    19421978    catch (NullPointerException e)
Note: See TracChangeset for help on using the changeset viewer.