@Test public void testTruncateNegative() throws IOException { FileChannel channel = channel(regularFile(0), READ, WRITE); try { channel.truncate(-1); fail(); } catch (IllegalArgumentException expected) { } }
@Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; }
JimfsFileChannel extends FileChannel { @Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; } }
JimfsFileChannel extends FileChannel { @Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); }
JimfsFileChannel extends FileChannel { @Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); AsynchronousFileChannel asAsynchronousFileChannel(ExecutorService executor); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int read(ByteBuffer dst, long position); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override int write(ByteBuffer src, long position); @Override long position(); @Override FileChannel position(long newPosition); @Override long size(); @Override FileChannel truncate(long size); @Override void force(boolean metaData); @Override long transferTo(long position, long count, WritableByteChannel target); @Override long transferFrom(ReadableByteChannel src, long position, long count); @Override MappedByteBuffer map(MapMode mode, long position, long size); @Override FileLock lock(long position, long size, boolean shared); @Override FileLock tryLock(long position, long size, boolean shared); }
JimfsFileChannel extends FileChannel { @Override public FileChannel truncate(long size) throws IOException { Util.checkNotNegative(size, "size"); checkOpen(); checkWritable(); synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return this; } file.writeLock().lockInterruptibly(); try { file.truncate(size); if (position > size) { position = size; } file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return this; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); AsynchronousFileChannel asAsynchronousFileChannel(ExecutorService executor); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int read(ByteBuffer dst, long position); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override int write(ByteBuffer src, long position); @Override long position(); @Override FileChannel position(long newPosition); @Override long size(); @Override FileChannel truncate(long size); @Override void force(boolean metaData); @Override long transferTo(long position, long count, WritableByteChannel target); @Override long transferFrom(ReadableByteChannel src, long position, long count); @Override MappedByteBuffer map(MapMode mode, long position, long size); @Override FileLock lock(long position, long size, boolean shared); @Override FileLock tryLock(long position, long size, boolean shared);  }