欢迎大家关注本博,同时欢迎大家评论交流,可以给个赞哦!!!
ServletInputStream和ServletOutputStream是Servlet规范API专门为ServletRequest和ServletResponse提供的输入和输出流。可以满足请求的读取和响应的写入。
ServletInputStream
ServletInputStream只提供了readLine方法,可以满足针对请求的按行读取,源码如下:
package javax.servlet;
import java.io.InputStream;
import java.io.IOException;
/**
* 提供用于从客户端请求读取二进制数据的输入流.
* 包括一次读取一行数据的高效readLine方法.
* 对于一些协议,比如HTTP POST和PUT,ServletInputStream对象可以用来读取从客户端发送的数据.
*/
public abstract class ServletInputStream extends InputStream {
/**
* Does nothing, because this is an abstract class.
*/
protected ServletInputStream() {
}
/**
* 封装安行读取的API.
* @param b 读取数据存储位置.
* @param off 偏移开始位置.
* @param len 读取数据长度.
* @return 读取结果.是否到达尾部.
* @throws IOException .
*/
public int readLine(byte[] b, int off, int len) throws IOException {
if (len <= 0) {
return 0;
}
int count = 0, c;
while ((c = read()) != -1) {
b[off++] = (byte) c;
count++;
if (c == '\n' || count == len) {
break;
}
}
return count > 0 ? count : -1;
}
}
ServletOutputStream
ServletOutputStream相对于ServletInputStream来说,提供的比较丰富,但阅读源码后,发现其定义都很简单,只是针对不同的需求,提供了不同数据类型的写入,源码如下:
package javax.servlet;
import java.io.OutputStream;
import java.io.IOException;
import java.io.CharConversionException;
import java.text.MessageFormat;
import java.util.ResourceBundle;
/**
* 提供用于向客户端发送二进制数据的输出流.
* ServletOutputStream对象通常通过ServletResponse的getOutputStream方法获取.
*/
public abstract class ServletOutputStream extends OutputStream {
private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE);
/**
* Does nothing, because this is an abstract class.
*/
protected ServletOutputStream() {
}
/**
* 向客户端写入一个String,末尾没有回车换行(CRLF)字符.
* @param s 待写入String.
* @throws IOException .
*/
public void print(String s) throws IOException {
if (s == null)
s = "null";
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
//
// XXX NOTE: This is clearly incorrect for many strings,
// but is the only consistent approach within the current
// servlet framework. It must suffice until servlet output
// streams properly encode their output.
//
if ((c & 0xff00) != 0) {
// high order byte must be zero
String errMsg = lStrings.getString("err.not_iso8859_1");
Object[] errArgs = new Object[1];
errArgs[0] = new Character(c);
errMsg = MessageFormat.format(errMsg, errArgs);
throw new CharConversionException(errMsg);
}
write(c);
}
}
/**
* 向客户端写入一个boolean,末尾没有回车换行(CRLF)字符.
* @param b 待写入boolean.
* @throws IOException .
*/
public void print(boolean b) throws IOException {
String msg;
if (b) {
msg = lStrings.getString("value.true");
} else {
msg = lStrings.getString("value.false");
}
print(msg);
}
/**
* 向客户端写入一个char,末尾没有回车换行(CRLF)字符.
* @param c 待写入char.
* @throws IOException .
*/
public void print(char c) throws IOException {
print(String.valueOf(c));
}
/**
* 向客户端写入一个int,末尾没有回车换行(CRLF)字符.
* @param i 待写入int.
* @throws IOException .
*/
public void print(int i) throws IOException {
print(String.valueOf(i));
}
/**
* 向客户端写入一个long,末尾没有回车换行(CRLF)字符.
* @param i 待写入long.
* @throws IOException .
*/
public void print(long l) throws IOException {
print(String.valueOf(l));
}
/**
* 向客户端写入一个float,末尾没有回车换行(CRLF)字符.
* @param i 待写入float.
* @throws IOException .
*/
public void print(float f) throws IOException {
print(String.valueOf(f));
}
/**
* 向客户端写入一个double,末尾没有回车换行(CRLF)字符.
* @param i 待写入double.
* @throws IOException .
*/
public void print(double d) throws IOException {
print(String.valueOf(d));
}
/**
* 向客户端写入回车换行(CRLF)字符.
* @throws IOException .
*/
public void println() throws IOException {
print("\r\n");
}
/**
* 向客户端写入一个String,末尾有回车换行(CRLF)字符.
* @param i 待写入String.
* @throws IOException .
*/
public void println(String s) throws IOException {
print(s);
println();
}
/**
* 向客户端写入一个boolean,末尾有回车换行(CRLF)字符.
* @param i 待写入boolean.
* @throws IOException .
*/
public void println(boolean b) throws IOException {
print(b);
println();
}
/**
* 向客户端写入一个char,末尾有回车换行(CRLF)字符.
* @param i 待写入char.
* @throws IOException .
*/
public void println(char c) throws IOException {
print(c);
println();
}
/**
* 向客户端写入一个int,末尾有回车换行(CRLF)字符.
* @param i 待写入int.
* @throws IOException .
*/
public void println(int i) throws IOException {
print(i);
println();
}
/**
* 向客户端写入一个long,末尾有回车换行(CRLF)字符.
* @param i 待写入long.
* @throws IOException .
*/
public void println(long l) throws IOException {
print(l);
println();
}
/**
* 向客户端写入一个float,末尾有回车换行(CRLF)字符.
* @param i 待写入float.
* @throws IOException .
*/
public void println(float f) throws IOException {
print(f);
println();
}
/**
* 向客户端写入一个double,末尾有回车换行(CRLF)字符.
* @param i 待写入double.
* @throws IOException .
*/
public void println(double d) throws IOException {
print(d);
println();
}
}
若文中存在错误和不足,欢迎指正!
本博微信公众号“超哥说码”,欢迎大家订阅,公众号正在完善中,会及时将更优质的博文推送于您!