@Override public StoreFileChannel truncate( long size ) throws IOException { channel.truncate( size ); return this; }
@Override public FileChannel truncate(long size) throws IOException { channel.truncate(size); return this; }
/** {@inheritDoc} */ @Override public void clear() throws IOException { ch.truncate(0); }
@Override public long doOp(FileChannel channel) throws IOException { channel.truncate(size); return 0; } });
@Override public SeekableByteChannel truncate(long size) throws IOException { channel.truncate(size); return this; }
public FileChannel truncate(final long size) throws IOException { return delegate.truncate(size); }
@Override public void truncate( File path, long size ) throws IOException { try ( FileChannel channel = FileChannel.open( path( path ) ) ) { channel.truncate( size ); } }
/** * Create a new file object output stream from the file channel. * * @param channel the file channel * @param append true for append mode, false for truncate and overwrite */ public FileChannelOutputStream(FileChannel channel, boolean append) throws IOException { this.channel = channel; if (append) { channel.position(channel.size()); } else { channel.position(0); channel.truncate(0); } }
@Override public synchronized FileChannel truncate(long newSize) throws IOException { cache.clear(); base.truncate(newSize); return this; }
@Override public FileChannel truncate(long newLength) throws IOException { for (int i = 0;; i++) { try { channel.truncate(newLength); return this; } catch (IOException e) { reopen(i, e); } } }
@Override public FileChannel truncate(long newLength) throws IOException { rec.log(Recorder.TRUNCATE, name, null, newLength); channel.truncate(newLength); return this; }
@Override public FileChannel truncate(long newLength) throws IOException { // compatibility with JDK FileChannel#truncate if (readOnly) { throw new NonWritableChannelException(); } /* * RandomAccessFile.setLength() does not always work here since Java 9 for * unknown reason so use FileChannel.truncate(). */ file.getChannel().truncate(newLength); return this; }
/** * Truncate the file. * * @param size the new file size */ public void truncate(long size) { try { writeCount.incrementAndGet(); file.truncate(size); fileSize = Math.min(fileSize, size); } catch (IOException e) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_WRITING_FAILED, "Could not truncate file {0} to size {1}", fileName, size, e); } }
@Override public FileChannel truncate(long newLength) throws IOException { rec.log(Recorder.TRUNCATE, name, null, newLength); channel.truncate(newLength); return this; }
@Override public FileChannel truncate(long newSize) throws IOException { cache.clear(); base.truncate(newSize); return this; }
/** * Truncate this file message set to the given size in bytes. Note that this API does no checking that the * given size falls on a valid message boundary. * In some versions of the JDK truncating to the same size as the file message set will cause an * update of the files mtime, so truncate is only performed if the targetSize is smaller than the * size of the underlying FileChannel. * It is expected that no other threads will do writes to the log when this function is called. * @param targetSize The size to truncate to. Must be between 0 and sizeInBytes. * @return The number of bytes truncated off */ public int truncateTo(int targetSize) throws IOException { int originalSize = sizeInBytes(); if (targetSize > originalSize || targetSize < 0) throw new KafkaException("Attempt to truncate log segment " + file + " to " + targetSize + " bytes failed, " + " size of this log segment is " + originalSize + " bytes."); if (targetSize < (int) channel.size()) { channel.truncate(targetSize); size.set(targetSize); } return originalSize - targetSize; }
LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException { this.targetFile = checkNotNull(resumable.targetFile()); this.tempFile = checkNotNull(resumable.tempFile()); if (!tempFile.exists()) { throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath()); } this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND); if (this.fileChannel.position() < resumable.offset()) { throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath()); } this.fileChannel.truncate(resumable.offset()); this.fos = Channels.newOutputStream(fileChannel); }
/** * see #testTruncateNotCalledIfSizeIsSameAsTargetSize */ @Test public void testTruncateIfSizeIsDifferentToTargetSize() throws IOException { FileChannel channelMock = mock(FileChannel.class); when(channelMock.size()).thenReturn(42L); when(channelMock.truncate(anyLong())).thenReturn(channelMock); FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false); fileRecords.truncateTo(23); verify(channelMock, atLeastOnce()).size(); verify(channelMock).truncate(23); }
/** * Class name for CustomClass class mapping file gets cleaned up from file system. */ private void corruptMarshallerStorage() throws Exception { String marshallerDir = U.defaultWorkDirectory() + File.separator + "marshaller"; File[] storedMappingsFiles = new File(marshallerDir).listFiles(); assert storedMappingsFiles.length == 1; try (FileOutputStream out = new FileOutputStream(storedMappingsFiles[0])) { out.getChannel().truncate(0); } }
/** * Test that truncateTo only calls truncate on the FileChannel if the size of the * FileChannel is bigger than the target size. This is important because some JVMs * change the mtime of the file, even if truncate should do nothing. */ @Test public void testTruncateNotCalledIfSizeIsSameAsTargetSize() throws IOException { FileChannel channelMock = mock(FileChannel.class); when(channelMock.size()).thenReturn(42L); when(channelMock.position(42L)).thenReturn(null); FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false); fileRecords.truncateTo(42); verify(channelMock, atLeastOnce()).size(); verify(channelMock, times(0)).truncate(anyLong()); }