chunkedcontentsource.java

来自「这是linux下ssl vpn的实现程序」· Java 代码 · 共 59 行

JAVA
59
字号
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 + =
减小字号Ctrl + -
显示快捷键?