Java Code Examples for java.nio.channels.AsynchronousFileChannel#read()
The following examples show how to use
java.nio.channels.AsynchronousFileChannel#read() .
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: ProgMainNio.java From javase with MIT License | 6 votes |
public void readFile(String filePath) throws IOException { Path path = Paths.get(filePath); AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, READ); ReadHandler handler = new ReadHandler(); int fileSize = (int) afc.size(); ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize); Attachment attach = new Attachment(); attach.asyncChannel = afc; attach.buffer = dataBuffer; attach.path = path; afc.read(dataBuffer, 0, attach, handler); System.out.println("Sleeping for 5 seconds..."); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 2
Source File: ProgMainNio.java From javase with MIT License | 6 votes |
private void readFile(String filePath) throws IOException { Path path = Paths.get(filePath); AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, READ); ReadHandler handler = new ReadHandler(); int fileSize = (int) afc.size(); ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize); Attachment attach = new Attachment(); attach.asyncChannel = afc; attach.buffer = dataBuffer; attach.path = path; afc.read(dataBuffer, 0, attach, handler); System.out.println("Sleeping for 5 seconds..."); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 3
Source File: FileChannelUtility.java From database with GNU General Public License v2.0 | 6 votes |
/** * Schedule a read on the channel. If the operation was previously * schedule and is done (normal completion), then return immediately. If * the operation was previously schedule and was cancelled, then throws * out a CancellationException. If the operation was previously schedule * and failed, then the future is cleared and the operation is * rescheduled. This is done in order to allow us to complete a high * level read on the channel when the backing channel may have been * closed by an interrupt in another thread due to Java IO channel * semantics (e.g., driven by query termination during reads). * * @param channel The channel. * * @throws IllegalArgumentException * @throws NonReadableChannelException * @throws CancellationException * @throws InterruptedException */ private void read(final AsynchronousFileChannel channel) throws IllegalArgumentException, NonReadableChannelException, CancellationException, InterruptedException { if (isDone()) { // Check for re-scheduling of the read(). try { /* * Note: It is either unlikely or impossible to have an * InterruptedException thrown out here since we know that * the Future isDone(). */ m_fut.get(); // throws CancellationException, ExecutionException, InterruptedException. } catch (ExecutionException ex) { /* * This read() had failed. We clear future so we can re-do * the read. */ m_fut = null; } } if(!isDone()) { // ensure buffer is ready m_buffer.reset(); m_fut = channel.read(m_buffer, m_addr); // throws IllegalArgumentException, NonReadableChannelException } }
Example 4
Source File: AsyncFileChannelIntegrationTest.java From tutorials with MIT License | 6 votes |
private String readContent(Path file) throws ExecutionException, InterruptedException { AsynchronousFileChannel fileChannel = null; try { fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ); } catch (IOException e) { e.printStackTrace(); } final ByteBuffer buffer = ByteBuffer.allocate(1024); final Future<Integer> operation = fileChannel.read(buffer, 0); operation.get(); final String fileContent = new String(buffer.array()).trim(); buffer.clear(); return fileContent; }
Example 5
Source File: FileRead.java From coroutines with Apache License 2.0 | 5 votes |
/*************************************** * {@inheritDoc} */ @Override protected boolean performAsyncOperation( int nBytesRead, AsynchronousFileChannel rChannel, ByteBuffer rData, ChannelCallback<Integer, AsynchronousFileChannel> rCallback) throws IOException { long nPosition = get(FILE_POSITION); boolean bFinished = false; if (nBytesRead >= 0) { bFinished = pCheckFinished.test(nBytesRead, rData); nPosition += nBytesRead; } if (nBytesRead != -1 && !bFinished && rData.hasRemaining()) { rChannel.read(rData, nPosition, rData, rCallback); } else // finished, either successfully or with an error { checkErrors(rData, nBytesRead, bFinished); // remove position in the case of a later restart deleteRelation(FILE_POSITION); rData.flip(); } return bFinished; }
Example 6
Source File: JimfsAsynchronousFileChannelTest.java From jimfs with Apache License 2.0 | 5 votes |
private static void checkAsyncRead(AsynchronousFileChannel channel) throws Throwable { ByteBuffer buf = buffer("1234567890"); assertEquals(10, (int) channel.read(buf, 0).get()); buf.flip(); SettableFuture<Integer> future = SettableFuture.create(); channel.read(buf, 0, null, setFuture(future)); assertThat(future.get(10, SECONDS)).isEqualTo(10); }
Example 7
Source File: AsyncFileChannelIntegrationTest.java From tutorials with MIT License | 4 votes |
@Test public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); final ByteBuffer buffer = ByteBuffer.allocate(1024); final Future<Integer> operation = fileChannel.read(buffer, 0); operation.get(); final String fileContent = new String(buffer.array()).trim(); buffer.clear(); assertEquals(fileContent, "baeldung.com"); }
Example 8
Source File: ProgMainNio.java From javase with MIT License | 2 votes |
public static void main(String[] args) { try { //Path path = Paths.get("data/nio-data.txt"); Path path = Paths.get("data/test-write2.txt"); AsynchronousFileChannel fileChannelW = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.allocate(1024); long positionW = 0; buffer.put("test data\r using Java SE NIO \r async with Future".getBytes()); buffer.flip(); Future<Integer> operationW = fileChannelW.write(buffer, positionW); buffer.clear(); while(!operationW.isDone()); System.out.println("Write done"); AsynchronousFileChannel fileChannelR = AsynchronousFileChannel.open(path, StandardOpenOption.READ); //ByteBuffer buffer = ByteBuffer.allocate(1024); buffer = ByteBuffer.allocate(1024); long positionR = 0; Future<Integer> operationR = fileChannelR.read(buffer, positionR); while(!operationR.isDone()); buffer.flip(); byte[] data = new byte[buffer.limit()]; buffer.get(data); System.out.println(new String(data)); buffer.clear(); } catch(IOException ioe) { ioe.printStackTrace(); } }