/** * Reads all the lines of this source as a list of strings. The returned list will be empty if * this source is empty. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source */ public ImmutableList<String> readLines() throws IOException { Closer closer = Closer.create(); try { BufferedReader reader = closer.register(openBufferedStream()); List<String> result = Lists.newArrayList(); String line; while ((line = reader.readLine()) != null) { result.add(line); } return ImmutableList.copyOf(result); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Reads the first line of this source as a string. Returns {@code null} if this source is empty. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source */ public @Nullable String readFirstLine() throws IOException { Closer closer = Closer.create(); try { BufferedReader reader = closer.register(openBufferedStream()); return reader.readLine(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
@MustBeClosed public Stream<String> lines() throws IOException { BufferedReader reader = openBufferedStream(); return reader .lines()
public void testOpenBufferedStream() throws IOException { BufferedReader reader = source.openBufferedStream(); StringWriter writer = new StringWriter(); char[] buf = new char[64]; int read; while ((read = reader.read(buf)) != -1) { writer.write(buf, 0, read); } reader.close(); writer.close(); assertExpectedString(writer.toString()); }
/** * Reads the first line of this source as a string. Returns {@code null} if this source is empty. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source */ @NullableDecl public String readFirstLine() throws IOException { Closer closer = Closer.create(); try { BufferedReader reader = closer.register(openBufferedStream()); return reader.readLine(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Reads all the lines of this source as a list of strings. The returned list will be empty if * this source is empty. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source */ public ImmutableList<String> readLines() throws IOException { Closer closer = Closer.create(); try { BufferedReader reader = closer.register(openBufferedStream()); List<String> result = Lists.newArrayList(); String line; while ((line = reader.readLine()) != null) { result.add(line); } return ImmutableList.copyOf(result); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
@MustBeClosed public Stream<String> lines() throws IOException { BufferedReader reader = openBufferedStream(); return reader .lines()
/** * Open a stream to read entities from this source. * @return A stream of entities. */ @Override public ObjectStream<Entity> openStream() throws IOException { BufferedReader reader = source.openBufferedStream(); ObjectStream<String> lines = new LineStream(reader); int headerLines = format.getHeaderLines(); List<String> header = new ArrayList<>(); while (header.size() < headerLines) { String line = lines.readObject(); if (line == null) { IOException ex = new IOException(String.format("%s: expected %d header lines, found %d", sourceURL, headerLines, header.size())); try { lines.close(); } catch (Throwable th) { ex.addSuppressed(th); } throw ex; } header.add(line); } LineEntityParser parser = format.makeParser(header); return ObjectStreams.transform(lines, parser); }
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(); while (input.hasNext()) { String name = input.nextName(); if (DESIRED_CAPABILITIES.equals(name)) { return input.read(MAP_TYPE); } input.skipValue(); } } return null; }
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(); 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; }
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(); 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; }
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(); 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; } } } }
public static BufferedReader asBufferedReader(String resource) { URL url = com.google.common.io.Resources.getResource(resource); try { CharSource charSource = com.google.common.io.Resources.asCharSource(url, Charsets.UTF_8); return charSource.openBufferedStream(); } catch (IOException ex) { throw new RuntimeException(ex); } } // {{end:asBufferedReader}}
static Reader readResource(final String path) throws DMPConverterException { try { return Resources .asCharSource(Resources.getResource(path), StandardCharsets.UTF_8) .openBufferedStream(); } catch (final IOException e) { throw new DMPConverterException("Could not read script resource", e); } }
public static Template compile(String name, CharSource charSource) { try (Reader reader = charSource.openBufferedStream()) { return createCompiler().compile(reader); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Read zero or more SAM records from the specified URL. * * @param url URL to read from, must not be null * @return zero or more SAM records read from the specified URL * @throws IOException if an I/O error occurs */ public static Iterable<SamRecord> records(final URL url) throws IOException { checkNotNull(url); try (BufferedReader reader = Resources.asCharSource(url, Charsets.UTF_8).openBufferedStream()) { return records(reader); } }
public static Template compile(String name, CharSource charSource) { try (Reader reader = charSource.openBufferedStream()) { return createCompiler().compile(reader); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public Reader reader() throws IOException { return IOHelper.toCharSource( this.resource.getBytes() ).openBufferedStream(); }
public void testOpenBufferedStream() throws IOException { BufferedReader reader = source.openBufferedStream(); StringWriter writer = new StringWriter(); char[] buf = new char[64]; int read; while ((read = reader.read(buf)) != -1) { writer.write(buf, 0, read); } reader.close(); writer.close(); assertExpectedString(writer.toString()); }
@Test public void testPragma() throws Exception { File file = new File("build/resources/test/pragma.c"); assertTrue(file.exists()); CharSource source = Files.asCharSource(file, Charsets.UTF_8); CppReader r = new CppReader(source.openBufferedStream()); r.getPreprocessor().setListener(new DefaultPreprocessorListener()); String output = CharStreams.toString(r); r.close(); LOG.info("Output: " + output); // assertTrue(output.contains("absolute-result")); } }