Java Code Examples for java.nio.channels.Channels#newReader()
The following examples show how to use
java.nio.channels.Channels#newReader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinuxProcFsStatistics.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
static int init() { nonPidFilesInProc = getNumberOfNonProcessProcFiles(); sys_cpus = Runtime.getRuntime().availableProcessors(); pageSize = Integer.getInteger(pageSizeProperty, DEFAULT_PAGESIZE); cpuStatSingleton = new CpuStat(); hasProcVmStat = new File("/proc/vmstat").exists(); hasDiskStats = new File("/proc/diskstats").exists(); st = new SpaceTokenizer(); procFile = new File( "/proc/" + NativeCalls.getInstance().getProcessId() + "/stat" ); try { fchannel = new FileInputStream(procFile).getChannel(); } catch (FileNotFoundException e) { throw new GemFireIOException(e.getMessage(), e); } procFileReader = Channels.newReader(fchannel, Charset.defaultCharset().newDecoder(), -1); return 0; }
Example 2
Source File: ExplicitShardedFile.java From beam with Apache License 2.0 | 6 votes |
/** * Reads all the lines of all the files. * * <p>Not suitable for use except in testing of small data, since the data size may be far more * than can be reasonably processed serially, in-memory, by a single thread. */ @VisibleForTesting List<String> readLines(Collection<Metadata> files) throws IOException { List<String> allLines = Lists.newArrayList(); int i = 1; for (Metadata file : files) { try (Reader reader = Channels.newReader(FileSystems.open(file.resourceId()), StandardCharsets.UTF_8.name())) { List<String> lines = CharStreams.readLines(reader); allLines.addAll(lines); LOG.debug("[{} of {}] Read {} lines from file: {}", i, files.size(), lines.size(), file); } i++; } return allLines; }
Example 3
Source File: FilePatternMatchingShardedFile.java From beam with Apache License 2.0 | 6 votes |
/** * Reads all the lines of all the files. * * <p>Not suitable for use except in testing of small data, since the data size may be far more * than can be reasonably processed serially, in-memory, by a single thread. */ @VisibleForTesting List<String> readLines(Collection<Metadata> files) throws IOException { List<String> allLines = Lists.newArrayList(); int i = 1; for (Metadata file : files) { try (Reader reader = Channels.newReader(FileSystems.open(file.resourceId()), StandardCharsets.UTF_8.name())) { List<String> lines = CharStreams.readLines(reader); allLines.addAll(lines); LOG.debug("[{} of {}] Read {} lines from file: {}", i, files.size(), lines.size(), file); } i++; } return allLines; }
Example 4
Source File: SchemaUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * The {@link SchemaUtils#getGcsFileAsString(String)} reads a file from GCS and returns it as a * string. * * @param filePath path to file in GCS * @return contents of the file as a string * @throws IOException thrown if not able to read file */ public static String getGcsFileAsString(String filePath) { MatchResult result; try { result = FileSystems.match(filePath); checkArgument( result.status() == MatchResult.Status.OK && !result.metadata().isEmpty(), "Failed to match any files with the pattern: " + filePath); List<ResourceId> rId = result.metadata().stream() .map(MatchResult.Metadata::resourceId) .collect(Collectors.toList()); checkArgument(rId.size() == 1, "Expected exactly 1 file, but got " + rId.size() + " files."); Reader reader = Channels.newReader(FileSystems.open(rId.get(0)), StandardCharsets.UTF_8.name()); return CharStreams.toString(reader); } catch (IOException ioe) { LOG.error("File system i/o error: " + ioe.getMessage()); throw new RuntimeException(ioe); } }
Example 5
Source File: CoopLockOperationDao.java From hadoop-connectors with Apache License 2.0 | 6 votes |
private void modifyOperationLock( String operationId, URI operationLockPath, Function<String, String> modifyFn) throws IOException { StorageResourceId lockId = StorageResourceId.fromUriPath(operationLockPath, /* allowEmptyObjectName= */ false); GoogleCloudStorageItemInfo lockInfo = gcs.getItemInfo(lockId); checkState(lockInfo.exists(), "lock file for %s operation should exist", operationId); String lock; try (BufferedReader reader = new BufferedReader(Channels.newReader(gcs.open(lockId), UTF_8.name()))) { lock = reader.lines().collect(Collectors.joining()); } lock = modifyFn.apply(lock); StorageResourceId lockIdWithGeneration = new StorageResourceId( lockId.getBucketName(), lockId.getObjectName(), lockInfo.getContentGeneration()); writeOperation(lockIdWithGeneration, UPDATE_OBJECT_OPTIONS, ImmutableList.of(lock)); }
Example 6
Source File: JsonExtract.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
/** * Switch to the next source file. * * @return * @throws JetelException */ private boolean nextSource() throws JetelException { ReadableByteChannel stream = null; while (readableChannelIterator.hasNext()) { autoFilling.resetSourceCounter(); autoFilling.resetGlobalSourceCounter(); stream = readableChannelIterator.nextChannel(); if (stream == null) continue; // if record no record found autoFilling.setFilename(readableChannelIterator.getCurrentFileName()); long fileSize = 0; Date fileTimestamp = null; if (autoFilling.getFilename() != null && !readableChannelIterator.isGraphDependentSource()) { try { File tmpFile = FileUtils.getJavaFile(getGraph().getRuntimeContext().getContextURL(), autoFilling.getFilename()); long timestamp = tmpFile.lastModified(); fileTimestamp = timestamp == 0 ? null : new Date(timestamp); fileSize = tmpFile.length(); } catch (Exception e) { //do nothing - the url is not regular file } } autoFilling.setFileSize(fileSize); autoFilling.setFileTimestamp(fileTimestamp); m_inputSource = new InputSource(Channels.newReader(stream, charset)); return true; } readableChannelIterator.blankRead(); return false; }
Example 7
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testNewReaderReadableByteChannelString() throws IOException { int bufSize = this.testNum; int readres = 0; CharBuffer charBuf = CharBuffer.allocate(bufSize); this.fins = new FileInputStream(tmpFile); ReadableByteChannel rbChannel = Channels.newChannel(this.fins); Reader testReader = Channels.newReader(rbChannel, Charset.forName( CODE_SET).newDecoder(), //$NON-NLS-1$ -1); Reader testReader_s = Channels.newReader(rbChannel, CODE_SET); //$NON-NLS-1$ assertEquals(this.fileSize, this.fins.available()); // not ready... assertTrue(testReader.ready()); assertTrue(testReader_s.ready()); // still reads readres = testReader.read(charBuf); assertEquals(bufSize, readres); assertEquals(0, this.fins.available()); try { readres = testReader.read((CharBuffer) null); fail(); } catch (NullPointerException e) { // correct } readres = testReader_s.read(charBuf); assertEquals(0, readres); assertTrue(testReader.ready()); assertTrue(testReader_s.ready()); }
Example 8
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testNewReaderReadableByteChannelString_internalBufferZero() throws IOException { int bufSize = this.testNum; int readres = 0; CharBuffer charBuf = CharBuffer.allocate(bufSize); this.fins = new FileInputStream(tmpFile); // channel null Reader testReader; try { testReader = Channels.newReader(null, Charset.forName(CODE_SET).newDecoder(), 0); assertNotNull(testReader); assertFalse(testReader.ready()); readres = testReader.read((CharBuffer) null); fail(); } catch (NullPointerException expected) { } assertEquals(0, readres); this.fins = null; // channel with null inputs try { ReadableByteChannel rbChannel = Channels.newChannel(this.fins); testReader = Channels.newReader(rbChannel, Charset.forName(CODE_SET).newDecoder(), -1); assertNotNull(testReader); assertFalse(testReader.ready()); readres = testReader.read(charBuf); fail(); } catch (NullPointerException e) { // correct } }
Example 9
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testnewReaderCharsetError() throws Exception { this.fins = new FileInputStream(tmpFile); ReadableByteChannel rbChannel = Channels.newChannel(this.fins); try { Channels.newReader(rbChannel, Charset.forName(BAD_CODE_SET) .newDecoder(), //$NON-NLS-1$ -1); fail(); } catch (UnsupportedCharsetException e) { // correct } }
Example 10
Source File: Playlist.java From AnimeTaste with MIT License | 5 votes |
private static Readable makeReadable(ReadableByteChannel source) { if (source == null) { throw new NullPointerException("source"); } return Channels.newReader(source, java.nio.charset.Charset.defaultCharset().name()); }
Example 11
Source File: LocalFileSystemTest.java From beam with Apache License 2.0 | 5 votes |
@Test public void testReadWithExistingFile() throws Exception { String expected = "my test string"; File existingFile = temporaryFolder.newFile(); Files.write(expected, existingFile, StandardCharsets.UTF_8); String data; try (Reader reader = Channels.newReader( localFileSystem.open( LocalResourceId.fromPath(existingFile.toPath(), false /* isDirectory */)), StandardCharsets.UTF_8.name())) { data = new LineReader(reader).readLine(); } assertEquals(expected, data); }
Example 12
Source File: CsvParser.java From SimpleFlatMapper with MIT License | 5 votes |
private static Reader newReader(File file, Charset charset) throws IOException { //IFJAVA8_START if (true) { FileChannel fileChannel = FileChannel.open(file.toPath()); try { return Channels.newReader(fileChannel, charset.newDecoder(), -1); } catch(Throwable e) { safeClose(fileChannel); throw e; } } //IFJAVA8_END return newReaderJava6(file, charset); }
Example 13
Source File: ReaderBenchmark.java From SimpleFlatMapper with MIT License | 5 votes |
@Benchmark public void testFileChannelViaRandomFile(Blackhole blackhole) throws IOException { try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) { try (FileChannel open = randomAccessFile.getChannel()) { try (Reader reader = Channels.newReader(open, "UTF-8")) { consume(reader, blackhole); } } } }
Example 14
Source File: ReaderBenchmark.java From SimpleFlatMapper with MIT License | 5 votes |
@Benchmark public void testFileChannel(Blackhole blackhole) throws IOException { try (FileChannel open = FileChannel.open(file.toPath())) { try (Reader reader = Channels.newReader(open, "UTF-8")) { consume(reader, blackhole); } } }
Example 15
Source File: CharReadingBenchmark.java From SimpleFlatMapper with MIT License | 5 votes |
@Benchmark public void reader(Blackhole blackhole) throws IOException { bufferSize = 4096 * 32; char[] buffer = new char[bufferSize]; try (FileChannel channel = FileChannel.open(file.toPath())) { try (Reader reader = Channels.newReader(channel, charset.newDecoder(), -1)) { int l; while ((l = reader.read(buffer)) != -1) { blackhole.consume(buffer); } } } }
Example 16
Source File: NetcatSource.java From mt-flume with Apache License 2.0 | 4 votes |
@Override public void run() { logger.debug("Starting connection handler"); Event event = null; try { Reader reader = Channels.newReader(socketChannel, "utf-8"); Writer writer = Channels.newWriter(socketChannel, "utf-8"); CharBuffer buffer = CharBuffer.allocate(maxLineLength); buffer.flip(); // flip() so fill() sees buffer as initially empty while (true) { // this method blocks until new data is available in the socket int charsRead = fill(buffer, reader); logger.debug("Chars read = {}", charsRead); // attempt to process all the events in the buffer int eventsProcessed = processEvents(buffer, writer); logger.debug("Events processed = {}", eventsProcessed); if (charsRead == -1) { // if we received EOF before last event processing attempt, then we // have done everything we can break; } else if (charsRead == 0 && eventsProcessed == 0) { if (buffer.remaining() == buffer.capacity()) { // If we get here it means: // 1. Last time we called fill(), no new chars were buffered // 2. After that, we failed to process any events => no newlines // 3. The unread data in the buffer == the size of the buffer // Therefore, we are stuck because the client sent a line longer // than the size of the buffer. Response: Drop the connection. logger.warn("Client sent event exceeding the maximum length"); counterGroup.incrementAndGet("events.failed"); writer.write("FAILED: Event exceeds the maximum length (" + buffer.capacity() + " chars, including newline)\n"); writer.flush(); break; } } } socketChannel.close(); counterGroup.incrementAndGet("sessions.completed"); } catch (IOException e) { counterGroup.incrementAndGet("sessions.broken"); } logger.debug("Connection handler exiting"); }
Example 17
Source File: FileChannelLinesSpliterator.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private BufferedReader getBufferedReader() { /** * A readable byte channel that reads bytes from an underlying * file channel over a specified range. */ ReadableByteChannel rrbc = new ReadableByteChannel() { @Override public int read(ByteBuffer dst) throws IOException { int bytesToRead = fence - index; if (bytesToRead == 0) return -1; int bytesRead; if (bytesToRead < dst.remaining()) { // The number of bytes to read is less than remaining // bytes in the buffer // Snapshot the limit, reduce it, read, then restore int oldLimit = dst.limit(); dst.limit(dst.position() + bytesToRead); bytesRead = fc.read(dst, index); dst.limit(oldLimit); } else { bytesRead = fc.read(dst, index); } if (bytesRead == -1) { index = fence; return bytesRead; } index += bytesRead; return bytesRead; } @Override public boolean isOpen() { return fc.isOpen(); } @Override public void close() throws IOException { fc.close(); } }; return new BufferedReader(Channels.newReader(rrbc, cs.newDecoder(), -1)); }
Example 18
Source File: FileChannelLinesSpliterator.java From Bytecoder with Apache License 2.0 | 4 votes |
private BufferedReader getBufferedReader() { /** * A readable byte channel that reads bytes from an underlying * file channel over a specified range. */ ReadableByteChannel rrbc = new ReadableByteChannel() { @Override public int read(ByteBuffer dst) throws IOException { int bytesToRead = fence - index; if (bytesToRead == 0) return -1; int bytesRead; if (bytesToRead < dst.remaining()) { // The number of bytes to read is less than remaining // bytes in the buffer // Snapshot the limit, reduce it, read, then restore int oldLimit = dst.limit(); dst.limit(dst.position() + bytesToRead); bytesRead = fc.read(dst, index); dst.limit(oldLimit); } else { bytesRead = fc.read(dst, index); } if (bytesRead == -1) { index = fence; return bytesRead; } index += bytesRead; return bytesRead; } @Override public boolean isOpen() { return fc.isOpen(); } @Override public void close() throws IOException { fc.close(); } }; return new BufferedReader(Channels.newReader(rrbc, cs.newDecoder(), -1)); }
Example 19
Source File: IntegrationTestHelper.java From dataflow-java with Apache License 2.0 | 4 votes |
/** * Open test output for reading for single file test results. */ public BufferedReader openOutput(String outputPath) throws IOException { return new BufferedReader(Channels.newReader(gcsUtil.open(GcsPath.fromUri(outputPath)), "UTF-8")); }
Example 20
Source File: NativeProcessTest.java From netbeans with Apache License 2.0 | 4 votes |
@org.junit.Test @ForAllEnvironments(section = "remote.platforms") public void testDestroySignal() throws Exception { for (int i = 1; i <= 5; i++) { System.out.println("testDestroySignal: Round " + i + " @ " + getTestExecutionEnvironment().getDisplayName()); // NOI18N NativeProcessBuilder npb = NativeProcessBuilder.newProcessBuilder(getTestExecutionEnvironment()); npb.getEnvironment().put("LC_ALL", "C"); // NOI18N npb.setExecutable("/bin/sh").setArguments("-c", "trap \"echo OK && exit\" TERM; echo ready; read X"); // NOI18N final NativeProcess process = npb.call(); assertEquals(State.RUNNING, process.getState()); final ReadableByteChannel channel = Channels.newChannel(process.getInputStream()); final BufferedReader br = new BufferedReader(Channels.newReader(channel, "UTF-8")); // NOI18N final Callable<String> lineReader = new Callable<String>() { @Override public String call() throws Exception { return br.readLine(); } }; String outputLine = getResult(lineReader, 2, TimeUnit.SECONDS); assertEquals("ready", outputLine); // NOI18N // Only after we have read 'ready' string we could be sure that // signal handler is installed... // Proceed with sending a signal. process.destroy(); // Signal should lead to process termination. getResult(new Callable<Void>() { @Override public Void call() throws Exception { process.waitFor(); return null; } }, 2, TimeUnit.SECONDS); assertNotSame(State.RUNNING, process.getState()); outputLine = getResult(lineReader, 2, TimeUnit.SECONDS); String error = ProcessUtils.readProcessErrorLine(process); assertEquals("OK", outputLine); // NOI18N assertEquals("", error); // NOI18N } }