Search in sources :

Example 6 with Sink

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");
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 7 with Sink

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");
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 8 with Sink

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);
}
Also used : Buffer(okio.Buffer) ByteBuffer(java.nio.ByteBuffer) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Source(okio.Source) RandomAccessFile(java.io.RandomAccessFile) Sink(okio.Sink) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 9 with Sink

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;
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Sink(okio.Sink) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Request(okhttp3.Request) BufferedSource(okio.BufferedSource)

Example 10 with Sink

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();
    }
}
Also used : Response(okhttp3.Response) Sink(okio.Sink) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) BufferedSource(okio.BufferedSource)

Aggregations

Sink (okio.Sink)15 BufferedSink (okio.BufferedSink)11 Buffer (okio.Buffer)9 Test (org.junit.Test)6 IOException (java.io.IOException)5 Request (okhttp3.Request)4 Response (okhttp3.Response)4 BufferedSource (okio.BufferedSource)4 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 OkHttpClient (okhttp3.OkHttpClient)2 ForwardingSink (okio.ForwardingSink)2 GzipSink (okio.GzipSink)2 Source (okio.Source)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1