📄 chunkedcontentsource.java
字号:
package com.maverick.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
/**
*
* @author not attributable
* @version 1.0
*/
public class ChunkedContentSource implements ContentSource {
ChunkedOutputStream actualOut = new ChunkedOutputStream();
HttpConnection con;
long bytesTransfered = 0;
public ChunkedContentSource() throws IOException {
}
public long getBytesTransferred() {
return bytesTransfered;
}
public void setHeaders(HttpRequest request, HttpConnection con) {
this.con = con;
request.setHeaderField("transfer-encoding", "chunked");
}
public OutputStream getOutputStream() {
return actualOut;
}
class ChunkedOutputStream extends OutputStream {
public void write(byte[] buf, int off, int len) throws IOException {
bytesTransfered+=len;
con.getOutputStream().write((Integer.toHexString(len) + "\r\n").getBytes("UTF8"));
con.getOutputStream().write(buf, off, len);
con.getOutputStream().write("\r\n".getBytes("UTF8"));
}
public void write(int b) throws IOException {
write(new byte[] { (byte)b}, 0 , 1);
}
public void close() throws IOException {
con.getOutputStream().write("0\r\n\r\n".getBytes("UTF8"));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -