本文整理了Java中java.nio.channels.FileChannel.truncate()
方法的一些代码示例,展示了FileChannel.truncate()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileChannel.truncate()
方法的具体详情如下:
包路径:java.nio.channels.FileChannel
类名称:FileChannel
方法名:truncate
[英]Truncates the file underlying this channel to a given size. Any bytes beyond the given size are removed from the file. If there are no bytes beyond the given size then the file contents are unmodified.
If the file position is currently greater than the given size, then it is set to the new size.
[中]将此通道下的文件截断为给定大小。超出给定大小的任何字节都将从文件中删除。如果没有超过给定大小的字节,则不会修改文件内容。
如果文件位置当前大于给定大小,则将其设置为新大小。
代码示例来源:origin: neo4j/neo4j
@Override
public StoreFileChannel truncate( long size ) throws IOException
{
channel.truncate( size );
return this;
}
代码示例来源:origin: wildfly/wildfly
@Override
public FileChannel truncate(long size) throws IOException {
channel.truncate(size);
return this;
}
代码示例来源:origin: apache/ignite
/** {@inheritDoc} */
@Override public void clear() throws IOException {
ch.truncate(0);
}
代码示例来源:origin: apache/geode
@Override
public long doOp(FileChannel channel) throws IOException {
channel.truncate(size);
return 0;
}
});
代码示例来源:origin: google/jimfs
@Override
public SeekableByteChannel truncate(long size) throws IOException {
channel.truncate(size);
return this;
}
代码示例来源:origin: wildfly/wildfly
public FileChannel truncate(final long size) throws IOException {
return delegate.truncate(size);
}
代码示例来源:origin: neo4j/neo4j
@Override
public void truncate( File path, long size ) throws IOException
{
try ( FileChannel channel = FileChannel.open( path( path ) ) )
{
channel.truncate( size );
}
}
代码示例来源:origin: com.h2database/h2
/**
* 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);
}
}
代码示例来源:origin: com.h2database/h2
@Override
public synchronized FileChannel truncate(long newSize) throws IOException {
cache.clear();
base.truncate(newSize);
return this;
}
代码示例来源:origin: com.h2database/h2
@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);
}
}
}
代码示例来源:origin: com.h2database/h2
@Override
public FileChannel truncate(long newLength) throws IOException {
rec.log(Recorder.TRUNCATE, name, null, newLength);
channel.truncate(newLength);
return this;
}
代码示例来源:origin: com.h2database/h2
@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;
}
代码示例来源:origin: com.h2database/h2
/**
* 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);
}
}
代码示例来源:origin: lealone/Lealone
@Override
public FileChannel truncate(long newLength) throws IOException {
rec.log(Recorder.TRUNCATE, name, null, newLength);
channel.truncate(newLength);
return this;
}
代码示例来源:origin: lealone/Lealone
@Override
public FileChannel truncate(long newSize) throws IOException {
cache.clear();
base.truncate(newSize);
return this;
}
代码示例来源:origin: apache/kafka
/**
* 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;
}
代码示例来源:origin: apache/flink
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);
}
代码示例来源:origin: apache/kafka
/**
* 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);
}
代码示例来源:origin: apache/ignite
/**
* 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);
}
}
代码示例来源:origin: apache/kafka
/**
* 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());
}
内容来源于网络,如有侵权,请联系作者删除!