📄 httpoutputstream.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.net.*;
import java.util.*;
public class HttpOutputStream extends BufferedOutputStream {
protected int code;
protected boolean sendHeaders, sendBody;
protected Hashtable headers = new Hashtable ();
public HttpOutputStream (OutputStream out, HttpInputStream in) {
super (out);
code = HTTP.STATUS_OKAY;
setHeader ("Server", HTTP.SERVER_INFO);
setHeader ("Date", new Date ().toString ());
sendHeaders = (in.getVersion () >= 1.0);
sendBody = !HTTP.METHOD_HEAD.equals (in.getMethod ());
}
public void setCode (int code) {
this.code = code;
}
public void setHeader (String attr, String value) {
headers.put (attr, value);
}
public boolean sendHeaders () throws IOException {
if (sendHeaders) {
write ("HTTP/1.0 " + code + " " + HTTP.getCodeMessage (code) + "\r\n");
Enumeration attrs = headers.keys ();
while (attrs.hasMoreElements ()) {
String attr = (String) attrs.nextElement ();
write (attr + ": " + headers.get (attr) + "\r\n");
}
write ('\n');
}
return sendBody;
}
public void write (String msg) throws IOException {
write (msg.getBytes ("latin1"));
}
public void write (InputStream in) throws IOException {
int n, length = buf.length;
while ((n = in.read (buf, count, length - count)) >= 0)
if ((count += n) >= length)
out.write (buf, count = 0, length);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -