📄 crlfinputstream.java
字号:
/*
* CRLFInputStream.java
* Copyright (C) 1999 dog <dog@dog.net.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* You may retrieve the latest version of this library from
* http://www.dog.net.uk/knife/
*/
package dog.mail.util;
import java.io.*;
/**
* An input stream that filters out CR/LF pairs into LFs.
*
* @author dog@dog.net.uk
* @version 1.0
*/
public class CRLFInputStream extends PushbackInputStream {
/**
* The CR octet.
*/
public static final int CR = 13;
/**
* The LF octet.
*/
public static final int LF = 10;
/**
* Constructs a CR/LF input stream connected to the specified input stream.
*/
public CRLFInputStream(InputStream in) {
super(in);
}
/**
* Constructs a CR/LF input stream connected to the specified input stream, with the specified pushback buffer size.
*/
public CRLFInputStream(InputStream in, int bufsize) {
super(in, bufsize);
}
/**
* Reads the next byte of data from this input stream.
* Returns -1 if the end of the stream has been reached.
* @exception IOException if an I/O error occurs
*/
public int read() throws IOException {
int c = super.read();
if (c==CR) // skip CR
return super.read();
return c;
}
/**
* Reads up to b.length bytes of data from this input stream into
* an array of bytes.
* Returns -1 if the end of the stream has been reached.
* @exception IOException if an I/O error occurs
*/
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
/**
* Reads up to len bytes of data from this input stream into an
* array of bytes, starting at the specified offset.
* Returns -1 if the end of the stream has been reached.
* @exception IOException if an I/O error occurs
*/
public int read(byte[] b, int off, int len) throws IOException {
int l = super.read(b, off, len);
return removeCRs(b, off, l);
}
/**
* Reads a line of input terminated by LF.
* @exception IOException if an I/O error occurs
*/
public String readLine() throws IOException {
byte[] acc = new byte[4096];
int count = 0;
for (byte b=(byte)read(); b!=LF && b!=-1; b=(byte)read())
acc[count++] = b;
if (count>0) {
byte[] bytes = new byte[count];
System.arraycopy(acc, 0, bytes, 0, count);
return new String(bytes);
} else
return null;
}
int removeCRs(byte[] b, int off, int len) {
for (int index = indexOfCR(b, off, len); index>-1; index = indexOfCR(b, off, len)) {
for (int i=index; i<b.length-1; i++)
b[i] = b[i+1];
len--;
}
return len;
}
int indexOfCR(byte[] b, int off, int len) {
for (int i=off; i<off+len; i++)
if (b[i]==CR) return i;
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -