@Override public void open(final int maxIO, final boolean useExecutor) throws IOException { try { channel = FileChannel.open(getFile().toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ); fileSize = channel.size(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public void open(final int maxIO, final boolean useExecutor) throws IOException { try { rfile = new RandomAccessFile(getFile(), "rw"); channel = rfile.getChannel(); fileSize = channel.size(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public long size() throws IOException { if (channel == null) { return getFile().length(); } try { return channel.size(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public void position(final long pos) throws IOException { try { super.position(pos); channel.position(pos); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public void position(final long pos) throws IOException { try { super.position(pos); channel.position(pos); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public void sync() throws IOException { if (factory.isDatasync() && channel != null) { try { channel.force(false); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } } }
@Override public void sync() throws IOException { if (factory.isDatasync() && channel != null) { try { channel.force(false); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } } }
@Override public long size() throws IOException { if (channel == null) { return getFile().length(); } try { return channel.size(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
@Override public synchronized void close() throws IOException, InterruptedException, ActiveMQException { super.close(); try { if (channel != null) { channel.close(); } } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } channel = null; notifyAll(); }
@Override public synchronized void close() throws IOException, InterruptedException, ActiveMQException { super.close(); try { if (channel != null) { channel.close(); } if (rfile != null) { rfile.close(); } } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } channel = null; rfile = null; notifyAll(); }
@Override public void run() { try { pushOpenedFile(); } catch (Exception e) { ActiveMQJournalLogger.LOGGER.errorPushingFile(e); fileFactory.onIOError(e, "unable to open ", null); } } };
@Override public void run() { try { pushOpenedFile(); } catch (Exception e) { ActiveMQJournalLogger.LOGGER.errorPushingFile(e); fileFactory.onIOError(e, "unable to open ", null); } } };
@Override public final void renameTo(final String newFileName) throws IOException, InterruptedException, ActiveMQException { try { close(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } File newFile = new File(directory + "/" + newFileName); if (!file.equals(newFile)) { if (!file.renameTo(newFile)) { throw ActiveMQJournalBundle.BUNDLE.ioRenameFileError(file.getName(), newFileName); } file = newFile; } }
@Override public final void renameTo(final String newFileName) throws IOException, InterruptedException, ActiveMQException { try { close(); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } File newFile = new File(directory + "/" + newFileName); if (!file.equals(newFile)) { if (!file.renameTo(newFile)) { throw ActiveMQJournalBundle.BUNDLE.ioRenameFileError(file.getName(), newFileName); } file = newFile; } }
@Override public int read(final ByteBuffer bytes, final IOCallback callback) throws ActiveMQException { checkOpened(); int bytesToRead = bytes.limit(); long positionToRead = position.getAndAdd(bytesToRead); bytes.rewind(); try { // We don't send the buffer to the callback on read, // because we want the buffer available. // Sending it through the callback would make it released aioFile.read(positionToRead, bytesToRead, bytes, getCallback(callback, null)); } catch (IOException e) { logger.error("IOError reading file: " + getFileName(), e); factory.onIOError(e, e.getMessage(), this); throw new ActiveMQNativeIOError(e.getMessage(), e); } return bytesToRead; }
@Override public int read(final ByteBuffer bytes, final IOCallback callback) throws ActiveMQException { checkOpened(); int bytesToRead = bytes.limit(); long positionToRead = position.getAndAdd(bytesToRead); bytes.rewind(); try { // We don't send the buffer to the callback on read, // because we want the buffer available. // Sending it through the callback would make it released aioFile.read(positionToRead, bytesToRead, bytes, getCallback(callback, null)); } catch (IOException e) { logger.error("IOError reading file: " + getFileName(), e); factory.onIOError(e, e.getMessage(), this); throw new ActiveMQNativeIOError(e.getMessage(), e); } return bytesToRead; }
@Override public synchronized void open(final int maxIO, final boolean useExecutor) throws ActiveMQException { opened = true; if (logger.isTraceEnabled()) { logger.trace("Opening file: " + getFileName()); } try { aioFile = aioFactory.libaioContext.openFile(getFile(), factory.isDatasync()); } catch (IOException e) { logger.error("Error opening file: " + getFileName()); factory.onIOError(e, e.getMessage(), this); throw new ActiveMQNativeIOError(e.getMessage(), e); } position.set(0); fileSize = aioFile.getSize(); }
@Override public synchronized void open(final int maxIO, final boolean useExecutor) throws ActiveMQException { opened = true; if (logger.isTraceEnabled()) { logger.trace("Opening file: " + getFileName()); } try { aioFile = aioFactory.libaioContext.openFile(getFile(), factory.isDatasync()); } catch (IOException e) { logger.error("Error opening file: " + getFileName()); factory.onIOError(e, e.getMessage(), this); throw new ActiveMQNativeIOError(e.getMessage(), e); } position.set(0); fileSize = aioFile.getSize(); }
@Override public synchronized int read(final ByteBuffer bytes, final IOCallback callback) throws IOException, ActiveMQIllegalStateException { try { if (channel == null) { throw new ActiveMQIllegalStateException("File " + this.getFileName() + " has a null channel"); } int bytesRead = channel.read(bytes); if (callback != null) { callback.done(); } bytes.flip(); return bytesRead; } catch (ClosedChannelException e) { throw e; } catch (IOException e) { if (callback != null) { callback.onError(ActiveMQExceptionType.IO_ERROR.getCode(), e.getLocalizedMessage()); } factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); throw e; } }
private void internalWrite(final ByteBuffer bytes, final boolean sync, final IOCallback callback, boolean releaseBuffer) throws IOException, ActiveMQIOErrorException, InterruptedException { if (!isOpen()) { if (callback != null) { callback.onError(ActiveMQExceptionType.IO_ERROR.getCode(), "File not opened"); } else { throw ActiveMQJournalBundle.BUNDLE.fileNotOpened(); } return; } position.addAndGet(bytes.limit()); try { doInternalWrite(bytes, sync, callback, releaseBuffer); } catch (ClosedChannelException e) { throw e; } catch (IOException e) { factory.onIOError(new ActiveMQIOErrorException(e.getMessage(), e), e.getMessage(), this); } }