⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomaccessreader.java

📁 StandBayeMail
💻 JAVA
字号:
/** * <p>Title: StandBayeMail </p> * <p>Description: A bayesian spam filter</p> * <p>Copyright: Copyright (c) 2004 by Luca M. Viola</p> * <p>Company: 3AM.it</p> * @author Luca M. Viola <luca@3am.it> * @version 1.0  This program is free software; you can redistribute it and/or  modify it under the terms of the GNU General Public License  as published by the Free Software Foundation; either version 2  of the License, or (at your option) any later version.  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License for more details.  You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package StandBayeMail;import java.io.*;/* This class is an implementation of a random access text reader, that could be buffered or not. Due to the Java API design, according to which FileReader does not support the mark() method, the unbuffered flavour has to use a RandomAccessFile, whereas the buffered one uses a BufferedReader and some magics behind-the-scene to get/set the filepointer. There is an inconsistency with the RandomAccessFile read method for chars (readChar), which expects to find 16-bit unicode chars and does't handle 8-bit ASCII streams properly, forcing the use of a custom made hack (cast of a readByte() to char) that makes the unbuffered flavour of this class unsuitable to use full unicode text files.*/public class RandomAccessReader{  private String filename;  private char mode;  //BufferedReader is character-oriented, RandomAccessFile byte-oriented.  //Unfortunately FileReader does not support the mark() method, so for  //random positioning with an unbuffered file we need to use RandomAccessFile  private BufferedReader br;  private RandomAccessFile raf;  private long length;  private long filepointer;  private char [] buf=new char[1];  public static final char MODE_BUFFERED='b';  public static final char MODE_UNBUFFERED='u';  public boolean isBuffered=true;  private RandomAccessReader()  {  }  public RandomAccessReader(String filename,char mode) throws IOException  {    this.filename=filename;    this.mode=mode;    File f=new File(filename);    if( f.exists() )      length=f.length();    else      throw new java.io.IOException("File not found "+filename);    switch( mode )    {        case MODE_BUFFERED:             isBuffered=true;             break;        case MODE_UNBUFFERED:             isBuffered=false;             break;        default:          isBuffered=false;          break;    }    if( isBuffered )    {      FileReader fr = new FileReader(f);      br = new BufferedReader(fr);      br.mark( (int) (length + 1));      filepointer=0L;    }    else      raf=new RandomAccessFile(f,"r");  }  public void close() throws java.io.IOException  {    if( isBuffered )      br.close();    else      raf.close();  }  public int read( char [] cbuf,int off,int len ) throws java.io.IOException  {    int i=0,j=0;    if( isBuffered )    {     int read=br.read(cbuf, off, len);     if( read>=0 )       filepointer+=read;     return read;    }    else      for( j=off; j<(off+len); j++ )        //This conversion *sucks* dry, but there would be        //no other way in hell to get a consistent char        //read from a RandomAccessFile with an 8bit ASCII        //file using RandomAccessFile.readChar()...        cbuf[j] = (char)raf.readByte();    return (j-off);  }  public char readChar()  throws java.io.IOException  {    if( isBuffered )    {      read(buf, 0, 1);      return buf[0];    }    return (char)raf.readByte();  }  public void seek(long pos) throws java.io.IOException  {    if( isBuffered )    {      br.reset();      br.skip(pos);      filepointer=pos;    }    else      raf.seek(pos);  }  public String readLine() throws java.io.IOException  {    if( isBuffered )    {      //If we used the original BufferedReader.readline()      //method (which is deprecated anyway) we couldn't      //have a clue about the new filepointer position,      //b/c the end-of line might be one or two characters      //but it doesn't get added to the resulting line.      //Therefore we handle it manually here...      StringBuffer input = new StringBuffer();      int c = -1;      boolean eol = false;      while (!eol)      {        int rd = read(buf, 0, 1);        if (rd < 0) c = -1;        else c = buf[0];        switch (c)        {          case -1:          case '\n':            eol = true;            break;          case '\r':            eol = true;            long cur = getFilePointer();            rd = read(buf, 0, 1);            if (rd < 0) c = -1;            else c = buf[0];            if ( (c) != '\n')              seek(cur);            break;          default:            input.append( (char) c);            break;        }      }      if ( (c == -1) && (input.length() == 0))        return null;      String read = input.toString();      return read;    }    return raf.readLine();  }  public long getFilePointer() throws java.io.IOException  {    if( isBuffered )      return filepointer;    return raf.getFilePointer();  }  public long length()  {    return length;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -