public void readLines(File file) throws IOException { try (Source fileSource = Okio.source(file); BufferedSource bufferedFileSource = Okio.buffer(fileSource)) { while (true) { String line = bufferedFileSource.readUtf8Line(); if (line == null) break; if (line.contains("square")) { System.out.println(line); } } } }
public static void main(String[] args) throws IOException { SocksProxyServer proxyServer = new SocksProxyServer(); proxyServer.start(); URL url = new URL("https://publicobject.com/helloworld.txt"); URLConnection connection = url.openConnection(proxyServer.proxy()); try (BufferedSource source = Okio.buffer(Okio.source(connection.getInputStream()))) { for (String line; (line = source.readUtf8Line()) != null; ) { System.out.println(line); } } proxyServer.shutdown(); } }
@WorkerThread private static Void loadFromAssets(Context context) throws IOException { InputStream stream = context.getAssets().open(AD_HOSTS_FILE); BufferedSource buffer = Okio.buffer(Okio.source(stream)); String line; while ((line = buffer.readUtf8Line()) != null) { AD_HOSTS.add(line); } buffer.close(); stream.close(); return null; }
@Override public void accept(BufferedSource source) { try { while (true) { String line = source.readUtf8Line(); if (line == null) { break; } resultCallback.onNext(objectMapper.readValue(line, typeReference)); } } catch (Exception e) { resultCallback.onError(e); } } }
private void readAdditionalCommands() throws IOException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("Received additional command: %s", command); }
private void readAdditionalCommands() throws IOException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("Received additional command: %s", command); }
private void readAdditionalCommands() throws IOException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("Received additional command: %s", command); }
@Test public void bufferedReaderCompatible() throws IOException { data.writeUtf8("abc\ndef"); assertEquals("abc", source.readUtf8Line()); assertEquals("def", source.readUtf8Line()); assertEquals(null, source.readUtf8Line()); }
@Test public void bufferedReaderCompatibleWithTrailingNewline() throws IOException { data.writeUtf8("abc\ndef\n"); assertEquals("abc", source.readUtf8Line()); assertEquals("def", source.readUtf8Line()); assertEquals(null, source.readUtf8Line()); } }
BufferedSource bufferedSource = Okio.buffer(fileSource)) { String temp; while ((temp = bufferedSource.readUtf8Line()) != null) { if ("".equals(temp.trim())) { break;
private void readExpectedCommand(ExpectedCommand expectedCommand) throws IOException, UnexpectedCommandException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("C: %s", command); String expected = expectedCommand.getCommand(); if (!command.equals(expected)) { logger.log("EXPECTED: %s", expected); throw new UnexpectedCommandException(expected, command); } }
private void readExpectedCommand(ExpectedCommand expectedCommand) throws IOException, UnexpectedCommandException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("C: %s", command); String expected = expectedCommand.getCommand(); if (!command.equals(expected)) { logger.log("EXPECTED: %s", expected); throw new UnexpectedCommandException(expected, command); } }
private void readExpectedCommand(ExpectedCommand expectedCommand) throws IOException, UnexpectedCommandException { String command = input.readUtf8Line(); if (command == null) { throw new EOFException(); } logger.log("C: %s", command); String expected = expectedCommand.getCommand(); if (!command.equals(expected)) { logger.log("EXPECTED: %s", expected); logger.log("ACTUAL: %s", command); throw new UnexpectedCommandException(expected, command); } }
private static void okio1() { try (BufferedSource source = Okio.buffer(Okio.source(new File("./26/text.txt")))) { System.out.println(source.readUtf8Line()); } catch (IOException e) { e.printStackTrace(); } }
public static Observable<String> events(BufferedSource source) { return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { while (!source.exhausted()) { subscriber.onNext(source.readUtf8Line()); } } catch (IOException e) { e.printStackTrace(); subscriber.onError(e); } subscriber.onCompleted(); } }); }
/** * @return the device total memory in byte */ public static long getTotalMemory() { File file = new File("/proc/meminfo"); BufferedSource source = null; try { // read meminfo file source = Okio.buffer(Okio.source(file)); String totalMemoryStr = source.readUtf8Line(); // get total memory String[] split = totalMemoryStr.split("\\s+"); return Integer.valueOf(split[1]).intValue() * 1024; } catch (Exception e) { e.printStackTrace(); } finally { try { if (source != null) { source.close(); } } catch (IOException e) { e.printStackTrace(); } } return 0; }
public Response<T> next() { try { String line = response.source().readUtf8Line(); if (line == null) { throw new RuntimeException("Null response from the server."); } try { return json.deserialize(line, watchType); } catch (JsonParseException ex) { Type statusType = new TypeToken<Response<V1Status>>() {}.getType(); Response<V1Status> status = json.deserialize(line, statusType); return new Response<T>(status.type, status.object); } } catch (IOException e) { throw new RuntimeException("IO Exception during next method.", e); } }
public Response<T> next() { try { String line = response.source().readUtf8Line(); if (line == null) { throw new RuntimeException("Null response from the server."); } try { return json.deserialize(line, watchType); } catch (JsonParseException ex) { Type statusType = new TypeToken<Response<V1Status>>() {}.getType(); Response<V1Status> status = json.deserialize(line, statusType); return new Response<T>(status.type, status.object); } } catch (IOException e) { throw new RuntimeException("IO Exception during next method.", e); } }
@Override public void call(Emitter<String> stringEmitter) { try { while (!source.exhausted()) { stringEmitter.onNext(source.readUtf8Line()); } stringEmitter.onCompleted(); } catch (IOException e) { stringEmitter.onError(e); } } }, BackpressureMode.BUFFER);