channels.remove(); try { final File f = new File(channel.getPath()); if (f.exists()) { f.delete();
/** * Creates a new {@link FileIOChannel.ID} out of all the tmp directories to avoid get cleared * unexpected. * * @param path The file to write * @return A channel in an directory out of all the tmp directories. */ public FileIOChannel.ID createChannel(File path) { final int threadNum = getNextThreadNum(); for (File localPath : paths) { if (path.getPath().startsWith(localPath.getPath())) { throw new RuntimeException(path.getPath() + " is not allowed in path: " + localPath.toString()); } } return new FileIOChannel.ID(path, threadNum); }
@Override public void readInto(Buffer buffer, long length) throws IOException { if (!withHeader) { if (fileChannel.size() - currentPosition > 0) { hasReachedEndOfFile = StreamFileChannelReader.readBufferFromFileChannel( fileChannel, buffer, currentPosition, length, id.getPathFile()); currentPosition += length; } else { buffer.recycleBuffer(); // In case of empty file. if (!hasReachedEndOfFile) { hasReachedEndOfFile = true; } } } else { throw new IllegalArgumentException("Method readInto(Buffer buffer, long length) only support withoutHeader mode."); } }
channels.remove(); try { final File f = new File(channel.getPath()); if (f.exists()) { f.delete();
channels.remove(); try { final File f = new File(channel.getPath()); if (f.exists()) { f.delete();
@Override public synchronized void close() { if (this.closed) { return; } this.closed = true; for (Iterator<FileIOChannel> channels = this.openChannels.iterator(); channels.hasNext(); ) { final FileIOChannel channel = channels.next(); channels.remove(); try { channel.closeAndDelete(); } catch (Throwable ignored) { } } for (Iterator<FileIOChannel.ID> channels = this.channels.iterator(); channels.hasNext(); ) { final FileIOChannel.ID channel = channels.next(); channels.remove(); try { final File f = new File(channel.getPath()); if (f.exists()) { f.delete(); } } catch (Throwable ignored) { } } }
/** * Creates a block channel reader that reads all blocks from the given channel directly in one bulk. * The reader draws segments to read the blocks into from a supplied list, which must contain as many * segments as the channel has blocks. After the reader is done, the list with the full segments can be * obtained from the reader. * <p> * If a channel is not to be read in one bulk, but in multiple smaller batches, a * {@link BlockChannelReader} should be used. * * @param channelID The descriptor for the channel to write to. * @param targetSegments The list to take the segments from into which to read the data. * @param numBlocks The number of blocks in the channel to read. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BulkBlockChannelReader createBulkBlockChannelReader(FileIOChannel.ID channelID, List<MemorySegment> targetSegments, int numBlocks) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBulkBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, targetSegments, numBlocks); }
/** * Creates a block channel reader that reads all blocks from the given channel directly in one bulk. * The reader draws segments to read the blocks into from a supplied list, which must contain as many * segments as the channel has blocks. After the reader is done, the list with the full segments can be * obtained from the reader. * <p> * If a channel is not to be read in one bulk, but in multiple smaller batches, a * {@link BlockChannelReader} should be used. * * @param channelID The descriptor for the channel to write to. * @param targetSegments The list to take the segments from into which to read the data. * @param numBlocks The number of blocks in the channel to read. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BulkBlockChannelReader createBulkBlockChannelReader(FileIOChannel.ID channelID, List<MemorySegment> targetSegments, int numBlocks) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBulkBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, targetSegments, numBlocks); }
/** * Creates a new channel to the path indicated by the given ID. The channel hands IO requests to * the given request queue to be processed. * * @param channelID The id describing the path of the file that the channel accessed. * @param writeEnabled Flag describing whether the channel should be opened in read/write mode, rather * than in read-only mode. * @throws IOException Thrown, if the channel could no be opened. */ protected AbstractFileIOChannel(FileIOChannel.ID channelID, boolean writeEnabled) throws IOException { this.id = Preconditions.checkNotNull(channelID); try { @SuppressWarnings("resource") RandomAccessFile file = new RandomAccessFile(id.getPath(), writeEnabled ? "rw" : "r"); this.fileChannel = file.getChannel(); } catch (IOException e) { throw new IOException("Channel to path '" + channelID.getPath() + "' could not be opened.", e); } }
/** * Creates a block channel reader that reads all blocks from the given channel directly in one bulk. * The reader draws segments to read the blocks into from a supplied list, which must contain as many * segments as the channel has blocks. After the reader is done, the list with the full segments can be * obtained from the reader. * <p> * If a channel is not to be read in one bulk, but in multiple smaller batches, a * {@link BlockChannelReader} should be used. * * @param channelID The descriptor for the channel to write to. * @param targetSegments The list to take the segments from into which to read the data. * @param numBlocks The number of blocks in the channel to read. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BulkBlockChannelReader createBulkBlockChannelReader(FileIOChannel.ID channelID, List<MemorySegment> targetSegments, int numBlocks) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBulkBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, targetSegments, numBlocks, bufferedReadSize); }
void clearFiles() { // we have to loop this, because it may fail with a concurrent modification exception while (!this.channelsToBeDelete.isEmpty()) { try { for (Iterator<FileIOChannel.ID> channels = this.channelsToBeDelete.iterator(); channels.hasNext(); ) { final FileIOChannel.ID channel = channels.next(); channels.remove(); try { final File f = new File(channel.getPath()); if (f.exists()) { f.delete(); } } catch (Throwable t) {} } } catch (Throwable t) {} } } }
/** * Creates a new channel to the path indicated by the given ID. The channel hands IO requests to * the given request queue to be processed. * * @param channelID The id describing the path of the file that the channel accessed. * @param writeEnabled Flag describing whether the channel should be opened in read/write mode, rather * than in read-only mode. * @throws IOException Thrown, if the channel could no be opened. */ protected AbstractFileIOChannel(FileIOChannel.ID channelID, boolean writeEnabled) throws IOException { this.id = Preconditions.checkNotNull(channelID); try { @SuppressWarnings("resource") RandomAccessFile file = new RandomAccessFile(id.getPath(), writeEnabled ? "rw" : "r"); this.fileChannel = file.getChannel(); } catch (IOException e) { throw new IOException("Channel to path '" + channelID.getPath() + "' could not be opened.", e); } }
/** * Creates a block channel reader that reads all blocks from the given channel directly in one bulk. * The reader draws segments to read the blocks into from a supplied list, which must contain as many * segments as the channel has blocks. After the reader is done, the list with the full segments can be * obtained from the reader. * <p> * If a channel is not to be read in one bulk, but in multiple smaller batches, a * {@link BlockChannelReader} should be used. * * @param channelID The descriptor for the channel to write to. * @param targetSegments The list to take the segments from into which to read the data. * @param numBlocks The number of blocks in the channel to read. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BulkBlockChannelReader createBulkBlockChannelReader(FileIOChannel.ID channelID, List<MemorySegment> targetSegments, int numBlocks) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBulkBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, targetSegments, numBlocks); }
/** * Creates a block channel reader that reads blocks from the given channel. The reader reads asynchronously, * such that a read request is accepted, carried out at some (close) point in time, and the full segment * is pushed to the given queue. * * @param channelID The descriptor for the channel to write to. * @param returnQueue The queue to put the full buffers into. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BlockChannelReader<MemorySegment> createBlockChannelReader(FileIOChannel.ID channelID, LinkedBlockingQueue<MemorySegment> returnQueue) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, returnQueue); }
@Override public void read() throws IOException { if (!hasReachedEndOfFile.get()) { if (bufferSize == -1) { hasReachedEndOfFile.set(StreamFileChannelReader.readBufferFromFileChannel( channel.fileChannel, buffer, startPosition, bytesToRead, channel.id.getPathFile())); } else { NioBufferedFileInputStream in = channel.getBufferedInputStream(bufferSize); if (in.available() > 0) { hasReachedEndOfFile.set(StreamFileChannelReader.readBufferFromFileBufferedChannel( in, buffer, startPosition, bytesToRead, channel.id.getPathFile())); } else { hasReachedEndOfFile.set(true); } } } }
/** * Creates a block channel reader that reads blocks from the given channel. The reader reads asynchronously, * such that a read request is accepted, carried out at some (close) point in time, and the full segment * is pushed to the given queue. * * @param channelID The descriptor for the channel to write to. * @param returnQueue The queue to put the full buffers into. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BlockChannelReader<MemorySegment> createBlockChannelReader(FileIOChannel.ID channelID, LinkedBlockingQueue<MemorySegment> returnQueue) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, returnQueue); }
/** * Creates a new channel to the path indicated by the given ID. The channel hands IO requests to * the given request queue to be processed. * * @param channelID The id describing the path of the file that the channel accessed. * @param writeEnabled Flag describing whether the channel should be opened in read/write mode, rather * than in read-only mode. * @throws IOException Thrown, if the channel could no be opened. */ protected AbstractFileIOChannel(FileIOChannel.ID channelID, boolean writeEnabled) throws IOException { this.id = Preconditions.checkNotNull(channelID); try { @SuppressWarnings("resource") RandomAccessFile file = new RandomAccessFile(id.getPath(), writeEnabled ? "rw" : "r"); this.fileChannel = file.getChannel(); } catch (IOException e) { throw new IOException("Channel to path '" + channelID.getPath() + "' could not be opened.", e); } }
/** * Creates a new channel to the path indicated by the given ID. The channel hands IO requests to * the given request queue to be processed. * * @param channelID The id describing the path of the file that the channel accessed. * @param writeEnabled Flag describing whether the channel should be opened in read/write mode, rather * than in read-only mode. * @throws IOException Thrown, if the channel could no be opened. */ protected AbstractFileIOChannel(FileIOChannel.ID channelID, boolean writeEnabled) throws IOException { this.id = Preconditions.checkNotNull(channelID); try { @SuppressWarnings("resource") RandomAccessFile file = new RandomAccessFile(id.getPath(), writeEnabled ? "rw" : "r"); this.fileChannel = file.getChannel(); } catch (IOException e) { throw new IOException("Channel to path '" + channelID.getPath() + "' could not be opened.", e); } }
/** * Creates a block channel reader that reads blocks from the given channel. The reader reads asynchronously, * such that a read request is accepted, carried out at some (close) point in time, and the full segment * is pushed to the given queue. * * @param channelID The descriptor for the channel to write to. * @param returnQueue The queue to put the full buffers into. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BlockChannelReader<MemorySegment> createBlockChannelReader(FileIOChannel.ID channelID, LinkedBlockingQueue<MemorySegment> returnQueue) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, returnQueue, bufferedReadSize); }
/** * Creates a block channel reader that reads blocks from the given channel. The reader reads asynchronously, * such that a read request is accepted, carried out at some (close) point in time, and the full segment * is pushed to the given queue. * * @param channelID The descriptor for the channel to write to. * @param returnQueue The queue to put the full buffers into. * @return A block channel reader that reads from the given channel. * @throws IOException Thrown, if the channel for the reader could not be opened. */ @Override public BlockChannelReader<MemorySegment> createBlockChannelReader(FileIOChannel.ID channelID, LinkedBlockingQueue<MemorySegment> returnQueue) throws IOException { checkState(!isShutdown.get(), "I/O-Manger is shut down."); return new AsynchronousBlockReader(channelID, this.readers[channelID.getThreadNum()].requestQueue, returnQueue); }