Search in sources :

Example 56 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class CacheTest method gzip.

/** Returns a gzipped copy of {@code bytes}. */
public Buffer gzip(String data) throws IOException {
    Buffer result = new Buffer();
    BufferedSink sink = Okio.buffer(new GzipSink(result));
    sink.writeUtf8(data);
    sink.close();
    return result;
}
Also used : Buffer(okio.Buffer) GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 57 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class SocksProxy method acceptCommand.

private void acceptCommand(InetAddress fromAddress, BufferedSource fromSource, BufferedSink fromSink) throws IOException {
    // Read the command.
    int version = fromSource.readByte() & 0xff;
    if (version != VERSION_5)
        throw new ProtocolException("unexpected version: " + version);
    int command = fromSource.readByte() & 0xff;
    int reserved = fromSource.readByte() & 0xff;
    if (reserved != 0)
        throw new ProtocolException("unexpected reserved: " + reserved);
    int addressType = fromSource.readByte() & 0xff;
    InetAddress toAddress;
    switch(addressType) {
        case ADDRESS_TYPE_IPV4:
            toAddress = InetAddress.getByAddress(fromSource.readByteArray(4L));
            break;
        case ADDRESS_TYPE_DOMAIN_NAME:
            int domainNameLength = fromSource.readByte() & 0xff;
            String domainName = fromSource.readUtf8(domainNameLength);
            // Resolve HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS to localhost.
            toAddress = domainName.equalsIgnoreCase(HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS) ? InetAddress.getByName("localhost") : InetAddress.getByName(domainName);
            break;
        default:
            throw new ProtocolException("unsupported address type: " + addressType);
    }
    int port = fromSource.readShort() & 0xffff;
    switch(command) {
        case COMMAND_CONNECT:
            Socket toSocket = new Socket(toAddress, port);
            byte[] localAddress = toSocket.getLocalAddress().getAddress();
            if (localAddress.length != 4) {
                throw new ProtocolException("unexpected address: " + toSocket.getLocalAddress());
            }
            // Write the reply.
            fromSink.writeByte(VERSION_5);
            fromSink.writeByte(REPLY_SUCCEEDED);
            fromSink.writeByte(0);
            fromSink.writeByte(ADDRESS_TYPE_IPV4);
            fromSink.write(localAddress);
            fromSink.writeShort(toSocket.getLocalPort());
            fromSink.emit();
            logger.log(Level.INFO, "SocksProxy connected " + fromAddress + " to " + toAddress);
            // Copy sources to sinks in both directions.
            BufferedSource toSource = Okio.buffer(Okio.source(toSocket));
            BufferedSink toSink = Okio.buffer(Okio.sink(toSocket));
            transfer(fromAddress, toAddress, fromSource, toSink);
            transfer(fromAddress, toAddress, toSource, fromSink);
            break;
        default:
            throw new ProtocolException("unexpected command: " + command);
    }
}
Also used : ProtocolException(java.net.ProtocolException) BufferedSink(okio.BufferedSink) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) BufferedSource(okio.BufferedSource)

Example 58 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class MultipartBodyTest method streamingPartHasNoLength.

@Test
public void streamingPartHasNoLength() throws Exception {
    class StreamingBody extends RequestBody {

        private final String body;

        StreamingBody(String body) {
            this.body = body;
        }

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(body);
        }
    }
    String expected = "" + "--123\r\n" + "Content-Length: 5\r\n" + "\r\n" + "Quick\r\n" + "--123\r\n" + "\r\n" + "Brown\r\n" + "--123\r\n" + "Content-Length: 3\r\n" + "\r\n" + "Fox\r\n" + "--123--\r\n";
    MultipartBody body = new MultipartBody.Builder("123").addPart(RequestBody.create(null, "Quick")).addPart(new StreamingBody("Brown")).addPart(RequestBody.create(null, "Fox")).build();
    assertEquals("123", body.boundary());
    assertEquals(MultipartBody.MIXED, body.type());
    assertEquals("multipart/mixed; boundary=123", body.contentType().toString());
    assertEquals(3, body.parts().size());
    assertEquals(-1, body.contentLength());
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    assertEquals(expected, buffer.readUtf8());
}
Also used : Buffer(okio.Buffer) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 59 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class InterceptorTest method gzip.

private Buffer gzip(String data) throws IOException {
    Buffer result = new Buffer();
    BufferedSink sink = Okio.buffer(new GzipSink(result));
    sink.writeUtf8(data);
    sink.close();
    return result;
}
Also used : Buffer(okio.Buffer) GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 60 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class FileOperatorTest method write.

private void write(ByteString data) throws IOException {
    BufferedSink sink = Okio.buffer(Okio.sink(file));
    sink.write(data);
    sink.close();
}
Also used : BufferedSink(okio.BufferedSink)

Aggregations

BufferedSink (okio.BufferedSink)84 Test (org.junit.Test)31 IOException (java.io.IOException)27 Buffer (okio.Buffer)18 File (java.io.File)13 RequestBody (okhttp3.RequestBody)13 Source (okio.Source)11 Response (okhttp3.Response)10 GzipSink (okio.GzipSink)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 BufferedSource (okio.BufferedSource)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3 Socket (java.net.Socket)3