/** * Closes the db immediately without saving last unsaved changes. * * [icon="{@docRoot}/note.png"] * NOTE: This operation is called from the JVM shutdown hook to * avoid database corruption. * */ void closeImmediately() { if (store != null) { try { store.closeImmediately(); context.shutdown(); } catch (NonWritableChannelException error) { if (!context.isReadOnly()) { log.error("Error while closing nitrite store.", error); } } catch (Throwable t) { log.error("Error while closing nitrite store.", t); } finally { store = null; log.info("Nitrite database has been closed by JVM shutdown hook without saving last unsaved changes."); } } else { log.error("Underlying store is null. Nitrite has not been initialized properly."); } }
public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { ensureOpen(); if (!src.isOpen()) throw new ClosedChannelException(); if (!writable) throw new NonWritableChannelException(); if ((position < 0) || (count < 0)) throw new IllegalArgumentException(); if (position > size()) return 0; if (src instanceof FileChannelImpl) return transferFromFileChannel((FileChannelImpl)src, position, count); return transferFromArbitraryChannel(src, position, count); }
public int write(ByteBuffer src, long position) throws IOException { if (src == null) throw new NullPointerException(); if (position < 0) throw new IllegalArgumentException("Negative position"); if (!writable) throw new NonWritableChannelException(); ensureOpen(); if (nd.needsPositionLock()) { synchronized (positionLock) { return writeInternal(src, position); } } else { return writeInternal(src, position); } }
public long transferTo(long position, long count, WritableByteChannel target) throws IOException { ensureOpen(); if (!target.isOpen()) throw new ClosedChannelException(); if (!readable) throw new NonReadableChannelException(); if (target instanceof FileChannelImpl && !((FileChannelImpl)target).writable) throw new NonWritableChannelException(); if ((position < 0) || (count < 0)) throw new IllegalArgumentException(); long sz = size(); if (position > sz) return 0; int icount = (int)Math.min(count, Integer.MAX_VALUE); if ((sz - position) < icount) icount = (int)(sz - position); // Slow path for untrusted targets return transferToArbitraryChannel(position, icount, target); }
@Override public synchronized int write(ByteBuffer src) throws IOException { try { int len; if (fileLength < pos + src.remaining()) { int length = (int) (fileLength - pos); int limit = src.limit(); src.limit(length); len = channel.write(src); src.limit(limit); pos += len; return len; } else { len = channel.write(src); pos += len; return len; } } catch (NonWritableChannelException e) { throw new IOException("read only"); } }
private <T> T throwException(String segmentName, Exception e) throws StreamSegmentException { if (e instanceof NoSuchFileException || e instanceof FileNotFoundException) { throw new StreamSegmentNotExistsException(segmentName); } if (e instanceof FileAlreadyExistsException) { throw new StreamSegmentExistsException(segmentName); } if (e instanceof IndexOutOfBoundsException) { throw new IllegalArgumentException(e.getMessage()); } if (e instanceof AccessControlException || e instanceof AccessDeniedException || e instanceof NonWritableChannelException) { throw new StreamSegmentSealedException(segmentName, e); } throw Lombok.sneakyThrow(e); }
public int write (ByteBuffer src, long position) throws IOException { if (position < 0) throw new IllegalArgumentException ("position: " + position); if (!isOpen ()) throw new ClosedChannelException (); if ((mode & WRITE) == 0) throw new NonWritableChannelException (); int result; long oldPosition; oldPosition = implPosition (); seek (position); result = write(src); seek (oldPosition); return result; }
private void lockCheck(long position, long size, boolean shared) throws IOException { if (position < 0 || size < 0) throw new IllegalArgumentException ("position: " + position + ", size: " + size); if (!isOpen ()) throw new ClosedChannelException(); if (shared && ((mode & READ) == 0)) throw new NonReadableChannelException(); if (!shared && ((mode & WRITE) == 0)) throw new NonWritableChannelException(); }
public FileChannel truncate (long size) throws IOException { if (size < 0) throw new IllegalArgumentException ("size: " + size); if (!isOpen ()) throw new ClosedChannelException (); if ((mode & WRITE) == 0) throw new NonWritableChannelException (); if (size < size ()) implTruncate (size); return this; }
private void insertAndDelete(File mpq, String filename) throws IOException { JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); Assert.assertFalse(mpqEditor.hasFile(filename)); try { String hashBefore = TestHelper.md5(mpq); mpqEditor.insertFile(filename, getFile(filename), true); mpqEditor.deleteFile(filename); mpqEditor.insertFile(filename, getFile(filename), false); mpqEditor.close(); String hashAfter = TestHelper.md5(mpq); // If this fails, the mpq is not changed by the insert file command and something went wrong Assert.assertNotEquals(hashBefore, hashAfter); mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); Assert.assertTrue(mpqEditor.hasFile(filename)); mpqEditor.deleteFile(filename); mpqEditor.close(); mpqEditor = new JMpqEditor(mpq, MPQOpenOption.READ_ONLY, MPQOpenOption.FORCE_V0); Assert.assertFalse(mpqEditor.hasFile(filename)); mpqEditor.close(); } catch (NonWritableChannelException ignored) { } }
@Override public FileChannel truncate(long size) throws IOException { throwExceptionIfClosed(); if(size < 0){ throw new MockIllegalArgumentException(); } if(!isOpenForWrite){ throw new NonWritableChannelException(); } long currentSize = size(); if(size < currentSize){ NativeMockedIO.setLength(path, position, size); } return this; }
/** * @tests serialization/deserialization compatibility. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationSelf", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "NonWritableChannelException", args = {} ) }) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new NonWritableChannelException()); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationGolden", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "NonWritableChannelException", args = {} ) }) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new NonWritableChannelException()); }
/** * @tests java.nio.channels.FileChannel#write(ByteBuffer[]) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies NonWritableChannelException", method = "write", args = {java.nio.ByteBuffer[].class} ) public void test_write$LByteBuffer_ReadOnly() throws Exception { ByteBuffer[] writeBuffers = new ByteBuffer[2]; writeBuffers[0] = ByteBuffer.allocate(CAPACITY); writeBuffers[1] = ByteBuffer.allocate(CAPACITY); try { readOnlyFileChannel.write(writeBuffers); fail("should throw NonWritableChannelException"); } catch (NonWritableChannelException e) { // expected } }
@Override public int write( ByteBuffer src ) throws IOException { if( !isOpen() ) { throw new ClosedChannelException(); } if( !options.contains( WRITE ) ) { throw new NonWritableChannelException(); } synchronized( this ) { int len = src.remaining(); // the cast below is ok because a DynamicByteArray can only hold Integer.MAX bytes (for now) //int incr = todo use it bytes.write( (int) position, src.array(), src.position(), len /*, 0 */); //store.getUsableSpace() ); //store.changeSize( -incr ); position += len; src.position( src.limit() ); watchEventListener.signal( ENTRY_MODIFY ); FileTime now = FileTime.from( Clock.systemUTC().instant() ); attis.setTimes( now, now, null ); return len; } }
@Override public FileChannel truncate( long size ) throws IOException { if( !isOpen() ) { throw new ClosedChannelException(); } if( !options.contains( WRITE ) ) { throw new NonWritableChannelException(); } if ( options.contains( APPEND )) { throw new FileSystemException( "can't truncate in append mode" ); } if( size < 0 ) { throw new IllegalArgumentException( "size must be non negative" ); } bytes.truncate( (int) size ); watchEventListener.signal( ENTRY_MODIFY ); return this; }
@Override public int write( ByteBuffer src ) throws IOException { if( !isOpen() ) { throw new ClosedChannelException(); } if( !options.contains( WRITE ) ) { throw new NonWritableChannelException(); } synchronized( this ) { int len = src.remaining(); // the cast below is ok because a DynamicByteArray can only hold Integer.MAX bytes (for now) int incr = bytes.write( (int) position, src.array(), src.position(), len, store.getUsableSpace() ); store.changeSize( -incr ); position += len; src.position( src.limit() ); // watchEventListener.signal( ENTRY_MODIFY ); attis.setLastAccessTime(); attis.setLastModifiedTime(); return len; } }
@Override public FileChannel truncate( long size ) throws IOException { if( !isOpen() ) { throw new ClosedChannelException(); } if( !options.contains( WRITE ) ) { throw new NonWritableChannelException(); } if ( options.contains( APPEND )) { throw new FileSystemException( "can't truncate in append mode" ); } if( size < 0 ) { throw new IllegalArgumentException( "size must be non negative" ); } bytes.truncate( (int) size ); // watchEventListener.signal( ENTRY_MODIFY ); return this; }