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;
}
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);
}
}
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());
}
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;
}
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();
}
Aggregations