Example usage for com.google.common.io CharSource openBufferedStream

List of usage examples for com.google.common.io CharSource openBufferedStream

Introduction

In this page you can find the example usage for com.google.common.io CharSource openBufferedStream.

Prototype

public BufferedReader openBufferedStream() throws IOException 

Source Link

Document

Opens a new BufferedReader for reading from this source.

Usage

From source file:org.openqa.selenium.remote.NewSessionPayload.java

private Map<String, Object> getAlwaysMatch() throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();//from   w w  w  .j av a 2 s .  co m
        while (input.hasNext()) {
            String name = input.nextName();
            if ("capabilities".equals(name)) {
                input.beginObject();
                while (input.hasNext()) {
                    name = input.nextName();
                    if ("alwaysMatch".equals(name)) {
                        return input.read(MAP_TYPE);
                    } else {
                        input.skipValue();
                    }
                }
                input.endObject();
            } else {
                input.skipValue();
            }
        }
    }
    return null;
}

From source file:org.openqa.selenium.remote.NewSessionPayload.java

private Collection<Map<String, Object>> getFirstMatches() throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();/*from   w w  w.j a v  a2s. c o m*/
        while (input.hasNext()) {
            String name = input.nextName();
            if ("capabilities".equals(name)) {
                input.beginObject();
                while (input.hasNext()) {
                    name = input.nextName();
                    if ("firstMatch".equals(name)) {
                        return input.read(new TypeToken<List<Map<String, Object>>>() {
                        }.getType());
                    } else {
                        input.skipValue();
                    }
                }
                input.endObject();
            } else {
                input.skipValue();
            }
        }
    }
    return null;
}

From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java

private @Nullable Map<String, Object> getOss() throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();//  w w  w  . j  av  a  2 s  .co m
        while (input.hasNext()) {
            String name = input.nextName();
            if (DESIRED_CAPABILITIES.equals(name)) {
                return input.read(MAP_TYPE);
            }
            input.skipValue();
        }
    }
    return null;
}

From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java

private void writeMetaData(JsonOutput out) throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();/*from www .  j a v a  2s .  c  o  m*/
        while (input.hasNext()) {
            String name = input.nextName();
            switch (name) {
            case CAPABILITIES:
            case DESIRED_CAPABILITIES:
            case REQUIRED_CAPABILITIES:
                input.skipValue();
                break;

            default:
                out.name(name);
                out.write(input.read(Object.class));
                break;
            }
        }
    }
}

From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java

private @Nullable Map<String, Object> getAlwaysMatch() throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();/* www .  j  a  va2s  .  co m*/
        while (input.hasNext()) {
            String name = input.nextName();
            if (CAPABILITIES.equals(name)) {
                input.beginObject();
                while (input.hasNext()) {
                    name = input.nextName();
                    if (ALWAYS_MATCH.equals(name)) {
                        return input.read(MAP_TYPE);
                    }
                    input.skipValue();
                }
                input.endObject();
            } else {
                input.skipValue();
            }
        }
    }
    return null;
}

From source file:io.appium.java_client.remote.NewAppiumSessionPayload.java

private @Nullable Collection<Map<String, Object>> getFirstMatch() throws IOException {
    CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
    try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) {
        input.beginObject();//  www  . j a  v a 2  s .com
        while (input.hasNext()) {
            String name = input.nextName();
            if (CAPABILITIES.equals(name)) {
                input.beginObject();
                while (input.hasNext()) {
                    name = input.nextName();
                    if (FIRST_MATCH.equals(name)) {
                        return input.read(LIST_OF_MAPS_TYPE);
                    }
                    input.skipValue();
                }
                input.endObject();
            } else {
                input.skipValue();
            }
        }
    }
    return null;
}

From source file:com.j2swift.util.ProGuardUsageParser.java

public static DeadCodeMap parse(CharSource listing) throws IOException {
    LineProcessor<DeadCodeMap> processor = new LineProcessor<DeadCodeMap>() {
        DeadCodeMap.Builder dead = DeadCodeMap.builder();
        String lastClass;// w  w  w  .  ja va2 s .c  o  m

        @Override
        public DeadCodeMap getResult() {
            return dead.build();
        }

        private void handleClass(String line) {
            if (line.endsWith(":")) {
                // Class, but not completely dead; save to read its dead methods
                lastClass = line.substring(0, line.length() - 1);
            } else {
                dead.addDeadClass(line);
            }
        }

        private void handleMethod(String line) throws IOException {
            Matcher methodMatcher = proGuardMethodPattern.matcher(line);
            if (!methodMatcher.matches()) {
                throw new AssertionError("Line doesn't match expected ProGuard format!");
            }
            if (lastClass == null) {
                throw new IOException("Bad listing format: method not attached to a class");
            }
            String returnType = methodMatcher.group(5);
            String methodName = methodMatcher.group(6);
            String arguments = methodMatcher.group(7);
            String signature = buildMethodSignature(returnType, arguments);
            dead.addDeadMethod(lastClass, methodName, signature);
        }

        private void handleField(String line) throws IOException {
            String name = line.substring(line.lastIndexOf(" ") + 1);
            dead.addDeadField(lastClass, name);
        }

        @Override
        public boolean processLine(String line) throws IOException {
            if (line.startsWith("ProGuard, version") || line.startsWith("Reading ")) {
                // ignore output header
            } else if (!line.startsWith("    ")) {
                handleClass(line);
            } else if (line.startsWith("    ") && !line.contains("(")) {
                handleField(line);
            } else {
                handleMethod(line);
            }
            return true;
        }
    };

    // TODO(cgdecker): Just use listing.readLines(processor) once guava_jdk5 is upgraded to a newer
    // version.
    Closer closer = Closer.create();
    try {
        BufferedReader reader = closer.register(listing.openBufferedStream());
        String line;
        while ((line = reader.readLine()) != null) {
            processor.processLine(line);
        }
        return processor.getResult();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}