Refine search
public void testConcat() throws IOException { CharSource c1 = CharSource.wrap("abc"); CharSource c2 = CharSource.wrap(""); CharSource c3 = CharSource.wrap("de"); String expected = "abcde"; assertEquals(expected, CharSource.concat(ImmutableList.of(c1, c2, c3)).read()); assertEquals(expected, CharSource.concat(c1, c2, c3).read()); assertEquals(expected, CharSource.concat(ImmutableList.of(c1, c2, c3).iterator()).read()); assertFalse(CharSource.concat(c1, c2, c3).isEmpty()); CharSource emptyConcat = CharSource.concat(CharSource.empty(), CharSource.empty()); assertTrue(emptyConcat.isEmpty()); }
public void testConcat_infiniteIterable() throws IOException { CharSource source = CharSource.wrap("abcd"); Iterable<CharSource> cycle = Iterables.cycle(ImmutableList.of(source)); CharSource concatenated = CharSource.concat(cycle); String expected = "abcdabcd"; // read the first 8 chars manually, since there's no equivalent to ByteSource.slice // TODO(cgdecker): Add CharSource.slice? StringBuilder builder = new StringBuilder(); Reader reader = concatenated.openStream(); // no need to worry about closing for (int i = 0; i < 8; i++) { builder.append((char) reader.read()); } assertEquals(expected, builder.toString()); }
public ExampleRecordCursor(List<ExampleColumnHandle> columnHandles, ByteSource byteSource) { this.columnHandles = columnHandles; fieldToColumnIndex = new int[columnHandles.size()]; for (int i = 0; i < columnHandles.size(); i++) { ExampleColumnHandle columnHandle = columnHandles.get(i); fieldToColumnIndex[i] = columnHandle.getOrdinalPosition(); } try (CountingInputStream input = new CountingInputStream(byteSource.openStream())) { lines = byteSource.asCharSource(UTF_8).readLines().iterator(); totalBytes = input.getCount(); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Returns whether the source has zero chars. The default implementation first checks {@link * #lengthIfKnown}, returning true if it's known to be zero and false if it's known to be * non-zero. If the length is not known, it falls back to opening a stream and checking for EOF. * * <p>Note that, in cases where {@code lengthIfKnown} returns zero, it is <i>possible</i> that * chars are actually available for reading. This means that a source may return {@code true} from * {@code isEmpty()} despite having readable content. * * @throws IOException if an I/O error occurs * @since 15.0 */ public boolean isEmpty() throws IOException { Optional<Long> lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent()) { return lengthIfKnown.get() == 0L; } Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return reader.read() == -1; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
private boolean shouldShutdown(final String message) { try { return shutdownKey.equals(wrap(message).readFirstLine()); } catch (IOException e) { throw new MocoException(e); } } }
public void test_ofUrl() throws Exception { File file = new File("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); URL url = file.toURI().toURL(); ResourceLocator test = ResourceLocator.ofUrl(url); String locator = test.getLocator(); assertTrue(locator.startsWith("url:file:")); assertTrue(locator.endsWith("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt")); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.toString(), locator); }
/** * 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(); } }
public void testSkipZero() throws Exception { CharSource source = newCharSource("a"); Iterable<CharSource> list = ImmutableList.of(source, source); Reader joinedReader = CharSource.concat(list).openStream(); assertEquals(0, joinedReader.skip(0)); assertEquals('a', joinedReader.read()); } }
@BeforeClass public void setup() throws Exception { Logging.initialize(); URL resource = getClass().getClassLoader().getResource("33.privateKey"); assertNotNull(resource, "key directory not found"); File keyDir = new File(resource.getFile()).getAbsoluteFile().getParentFile(); defaultKey = getMimeDecoder().decode(asCharSource(new File(keyDir, "default-key.key"), US_ASCII).read().getBytes(US_ASCII)); hmac222 = getMimeDecoder().decode(asCharSource(new File(keyDir, "222.key"), US_ASCII).read().getBytes(US_ASCII)); privateKey33 = PemReader.loadPrivateKey(new File(keyDir, "33.privateKey"), Optional.empty()); server = new TestingPrestoServer( true, ImmutableMap.<String, String>builder() .put("http-server.authentication.type", "JWT") .put("http.authentication.jwt.key-file", new File(keyDir, "${KID}.key").toString()) .put("http-server.https.enabled", "true") .put("http-server.https.keystore.path", getResource("localhost.keystore").getPath()) .put("http-server.https.keystore.key", "changeit") .build(), null, null, new SqlParserOptions(), ImmutableList.of()); server.installPlugin(new TpchPlugin()); server.createCatalog(TEST_CATALOG, "tpch"); waitForNodeRefresh(server); }
/** * Reads the contents of this source as a string. * * @throws IOException if an I/O error occurs while reading from this source */ public String read() throws IOException { Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.toString(reader); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Appends the contents of this source to the given {@link Appendable} (such as a {@link Writer}). * Does not close {@code appendable} if it is {@code Closeable}. * * @return the number of characters copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * appendable} */ @CanIgnoreReturnValue public long copyTo(Appendable appendable) throws IOException { checkNotNull(appendable); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.copy(reader, appendable); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Reads lines of text from this source, processing each line as it is read using the given {@link * LineProcessor processor}. Stops when all lines have been processed or the processor returns * {@code false} and returns the result produced by the processor. * * <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 or if {@code * processor} throws an {@code IOException} * @since 16.0 */ @Beta @CanIgnoreReturnValue // some processors won't return a useful result public <T> T readLines(LineProcessor<T> processor) throws IOException { checkNotNull(processor); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.readLines(reader, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void test_of_empty_no_header() { CsvFile csvFile = CsvFile.of(CharSource.wrap(""), false); assertEquals(csvFile.headers().size(), 0); assertEquals(csvFile.rowCount(), 0); assertEquals(csvFile.containsHeader("Foo"), false); assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false); }
public void testOfFile() throws Exception { CharSource charSource = CharSources.ofFile(new File(fileName)); assertEquals(charSource.readFirstLine(), "H\u0000e\u0000l\u0000l\u0000o\u0000"); assertEquals(charSource.length(), 10); }
public void testReadFirstLine() throws IOException { if (expectedLines.isEmpty()) { assertNull(source.readFirstLine()); } else { assertEquals(expectedLines.get(0), source.readFirstLine()); } }
@Test public void testOfUrl() throws Exception { String fullPathToFile = "file:///" + System.getProperty("user.dir") + "/" + fileName; URL url = new URL(fullPathToFile); CharSource charSource = CharSources.ofUrl(url); assertEquals(charSource.readFirstLine(), "H\u0000e\u0000l\u0000l\u0000o\u0000"); }
private JsonParser.Stream newJsonStream(FileInputInputStream in, PluginTask task) throws IOException { InvalidEscapeStringPolicy policy = task.getInvalidEscapeStringPolicy(); switch (policy) { case SKIP: case UNESCAPE: Iterable<CharSource> lines = Lists.transform(CharStreams.readLines(new BufferedReader(new InputStreamReader(in))), invalidEscapeStringFunction(policy)); return new JsonParser().open(new ByteArrayInputStream(CharStreams.toString(CharSource.concat(lines).openStream()).getBytes(StandardCharsets.UTF_8))); case PASSTHROUGH: default: return new JsonParser().open(in); } }
public void testSimple() throws Exception { String testString = "abcdefgh"; CharSource source = newCharSource(testString); Reader joinedReader = CharSource.concat(source, source).openStream(); String expectedString = testString + testString; assertEquals(expectedString, CharStreams.toString(joinedReader)); }