use of okio.Sink in project okhttp by square.
the class WebSocketWriterTest method serverMessageLengthShort.
@Test
public void serverMessageLengthShort() throws IOException {
Sink sink = serverWriter.newMessageSink(OPCODE_BINARY, -1);
// Create a payload which will overflow the normal payload byte size.
Buffer payload = new Buffer();
while (payload.completeSegmentByteCount() <= PAYLOAD_BYTE_MAX) {
payload.writeByte('0');
}
long byteCount = payload.completeSegmentByteCount();
// Write directly to the unbuffered sink. This ensures it will become single frame.
sink.write(payload.clone(), byteCount);
// 'e' == 4-byte follow-up length.
assertData("027e");
assertData(Util.format("%04X", payload.completeSegmentByteCount()));
assertData(payload.readByteArray());
sink.close();
assertData("8000");
}
use of okio.Sink in project okhttp by square.
the class WebSocketWriterTest method serverMessageLengthLong.
@Test
public void serverMessageLengthLong() throws IOException {
Sink sink = serverWriter.newMessageSink(OPCODE_BINARY, -1);
// Create a payload which will overflow the normal and short payload byte size.
Buffer payload = new Buffer();
while (payload.completeSegmentByteCount() <= PAYLOAD_SHORT_MAX) {
payload.writeByte('0');
}
long byteCount = payload.completeSegmentByteCount();
// Write directly to the unbuffered sink. This ensures it will become single frame.
sink.write(payload.clone(), byteCount);
// 'f' == 16-byte follow-up length.
assertData("027f");
assertData(Util.format("%016X", byteCount));
assertData(payload.readByteArray(byteCount));
sink.close();
assertData("8000");
}
use of okio.Sink in project FileDownloader by lingochamp.
the class PerformanceTestActivity method onClickWriteTest.
public void onClickWriteTest(final View view) {
FileOutputStream fos = null;
InputStream inputStream = initPerformanceTest();
byte[] buff = new byte[BUFFER_SIZE];
long start = System.currentTimeMillis();
int tenthMilliSec = ioPerformanceSb.getProgress();
int sleepMilliSec = tenthMilliSec / 10;
int sleepNanoSec = (tenthMilliSec - (tenthMilliSec / 10) * 10) * TENTH_MILLI_TO_NANO;
infoTv.append(String.format("Output test with %.1f ms extra operate\n", tenthMilliSec / 10.0f));
// ---------------------- FileOutputStream
try {
fos = new FileOutputStream(writePerformanceTestPath, true);
do {
int byteCount = inputStream.read(buff);
if (byteCount == -1) {
break;
}
fos.write(buff, 0, byteCount);
if (sleepMilliSec > 0 || sleepNanoSec > 0) {
try {
Thread.sleep(sleepMilliSec, sleepNanoSec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} while (true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.getFD().sync();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
infoAppend("FileOutputStream", start);
BufferedOutputStream bos = null;
inputStream = initPerformanceTest();
start = System.currentTimeMillis();
// ---------------------- BufferedOutputStream
try {
bos = new BufferedOutputStream(new FileOutputStream(writePerformanceTestPath, true));
do {
int byteCount = inputStream.read(buff);
if (byteCount == -1) {
break;
}
bos.write(buff, 0, byteCount);
if (sleepMilliSec > 0 || sleepNanoSec > 0) {
try {
Thread.sleep(sleepMilliSec, sleepNanoSec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} while (true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
infoAppend("BufferOutputStream", start);
RandomAccessFile raf = null;
inputStream = initPerformanceTest();
start = System.currentTimeMillis();
// ---------------------- RandomAccessFile
try {
raf = new RandomAccessFile(writePerformanceTestPath, "rw");
do {
int byteCount = inputStream.read(buff);
if (byteCount == -1) {
break;
}
raf.write(buff, 0, byteCount);
if (sleepMilliSec > 0 || sleepNanoSec > 0) {
try {
Thread.sleep(sleepMilliSec, sleepNanoSec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} while (true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
infoAppend("RandomAccessFile", start);
Sink sink = null;
inputStream = initPerformanceTest();
Source source = Okio.source(inputStream);
Buffer buffer = new Buffer();
start = System.currentTimeMillis();
try {
sink = Okio.sink(new File(writePerformanceTestPath));
sink = Okio.buffer(sink);
do {
long byteCount = source.read(buffer, BUFFER_SIZE);
if (byteCount == -1) {
break;
}
sink.write(buffer, byteCount);
if (sleepMilliSec > 0 || sleepNanoSec > 0) {
try {
Thread.sleep(sleepMilliSec, sleepNanoSec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} while (true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sink != null) {
try {
sink.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
infoAppend("okio", start);
}
use of okio.Sink in project uCrop by Yalantis.
the class BitmapLoadTask method downloadFile.
private void downloadFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws NullPointerException, IOException {
Log.d(TAG, "downloadFile");
if (outputUri == null) {
throw new NullPointerException("Output Uri is null - cannot download image");
}
OkHttpClient client = new OkHttpClient();
BufferedSource source = null;
Sink sink = null;
Response response = null;
try {
Request request = new Request.Builder().url(inputUri.toString()).build();
response = client.newCall(request).execute();
source = response.body().source();
OutputStream outputStream = mContext.getContentResolver().openOutputStream(outputUri);
if (outputStream != null) {
sink = Okio.sink(outputStream);
source.readAll(sink);
} else {
throw new NullPointerException("OutputStream for given output Uri is null");
}
} finally {
BitmapLoadUtils.close(source);
BitmapLoadUtils.close(sink);
if (response != null) {
BitmapLoadUtils.close(response.body());
}
client.dispatcher().cancelAll();
// swap uris, because input image was downloaded to the output destination
// (cropped image will override it later)
mInputUri = mOutputUri;
}
}
use of okio.Sink in project okhttp by square.
the class Main method run.
@Override
public void run() {
if (showHelpIfRequested()) {
return;
}
if (version) {
System.out.println(NAME + " " + versionString());
System.out.println("Protocols: " + protocols());
return;
}
if (showHttp2Frames) {
enableHttp2FrameLogging();
}
client = createClient();
Request request = createRequest();
try {
Response response = client.newCall(request).execute();
if (showHeaders) {
System.out.println(StatusLine.get(response));
Headers headers = response.headers();
for (int i = 0, size = headers.size(); i < size; i++) {
System.out.println(headers.name(i) + ": " + headers.value(i));
}
System.out.println();
}
// Stream the response to the System.out as it is returned from the server.
Sink out = Okio.sink(System.out);
BufferedSource source = response.body().source();
while (!source.exhausted()) {
out.write(source.buffer(), source.buffer().size());
out.flush();
}
response.body().close();
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
Aggregations