Refine search
public static void toFile(@NonNull ByteBuffer buffer, @NonNull File file) throws IOException { buffer.position(0); RandomAccessFile raf = null; FileChannel channel = null; try { raf = new RandomAccessFile(file, "rw"); channel = raf.getChannel(); channel.write(buffer); channel.force(false /*metadata*/); channel.close(); raf.close(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { // Ignored. } } if (raf != null) { try { raf.close(); } catch (IOException e) { // Ignored. } } } }
/** * Write node id (who captured lock) into lock file. * * @param content Node id. * @throws IOException if some fail while write node it. */ private void writeContent(String content) throws IOException { FileChannel ch = lockFile.getChannel(); byte[] bytes = content.getBytes(); ByteBuffer buf = ByteBuffer.allocate(bytes.length); buf.put(bytes); buf.flip(); ch.write(buf, 1); ch.force(false); }
private File createJournalTemplateFile() { String fileName = "db-log.template"; File rc = new File(directory, fileName); try (RandomAccessFile templateRaf = new RandomAccessFile(rc, "rw");) { templateRaf.getChannel().write(ByteBuffer.wrap(EOF_RECORD)); templateRaf.setLength(maxFileLength); templateRaf.getChannel().force(true); } catch (FileNotFoundException e) { LOG.error("Could not find the template file on disk at " + osKernelCopyTemplateFile.getAbsolutePath(), e); } catch (IOException e) { LOG.error("Could not transfer the template file to journal, transferFile=" + osKernelCopyTemplateFile.getAbsolutePath(), e); } return rc; }
private void doPreallocationZeros(RecoverableRandomAccessFile file) { preAllocateDirectBuffer.rewind(); try { FileChannel channel = file.getChannel(); channel.write(preAllocateDirectBuffer); channel.force(false); channel.position(0); } catch (ClosedByInterruptException ignored) { LOG.trace("Could not preallocate journal file with zeros", ignored); } catch (IOException e) { LOG.error("Could not preallocate journal file with zeros", e); } }
/** * Write file. * * @param fullFileName the full file name * @param contents the contents */ public static void writeFile(final String fullFileName, final byte[] contents) { try { RandomAccessFile raf = new RandomAccessFile(fullFileName, "rw"); try (FileChannel channel = raf.getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(contents.length); buffer.put(contents); buffer.flip(); while (buffer.hasRemaining()) { channel.write(buffer); } channel.force(true); } } catch (IOException e) { e.printStackTrace(); } } }
private AllocatedPort lock(final int port, final String name) { final Path path = lockDirectory.resolve(String.valueOf(port)); try { final FileChannel file = FileChannel.open(path, CREATE, WRITE); final FileLock lock = file.tryLock(); if (lock == null) { return null; } file.write(ByteBuffer.wrap(format("%d %s%n", port, name).getBytes(UTF_8))); file.force(true); return new AllocatedPort(port, path, file, lock); } catch (OverlappingFileLockException e) { return null; } catch (IOException e) { log.error("Failed to take port lock: {}", path, e); throw new RuntimeException(e); } }
private void writeFile(final TransactionRecoverAdapter adapter, final String fullFileName) { try { RandomAccessFile raf = new RandomAccessFile(fullFileName, "rw"); try (FileChannel channel = raf.getChannel()) { byte[] content = objectSerializer.serialize(adapter); ByteBuffer buffer = ByteBuffer.allocate(content.length); buffer.put(content); buffer.flip(); while (buffer.hasRemaining()) { channel.write(buffer); } channel.force(true); } } catch (Exception e) { e.printStackTrace(); } }
private void doPreallocationSparseFile(RecoverableRandomAccessFile file) { final ByteBuffer journalEof = ByteBuffer.wrap(EOF_RECORD); try { FileChannel channel = file.getChannel(); channel.position(0); channel.write(journalEof); channel.position(maxFileLength - 5); journalEof.rewind(); channel.write(journalEof); channel.force(false); channel.position(0); } catch (ClosedByInterruptException ignored) { LOG.trace("Could not preallocate journal file with sparse file", ignored); } catch (IOException e) { LOG.error("Could not preallocate journal file with sparse file", e); } }
/** * {@inheritDoc} */ @Override protected void addResource(XAResource resource, URI uri) throws SystemException { assert fileChannel != null; try { assert fileChannel.isOpen(); fileChannel.write(ByteBuffer.wrap((uri.toString() + System.lineSeparator()).getBytes(StandardCharsets.UTF_8))); fileChannel.force(true); } catch (IOException e) { throw Log.log.appendXAResourceRecoveryFileFailed(uri, filePath, e); } this.resources.add(resource); Log.log.xaResourceAddedToRecoveryRegistry(uri, filePath); }
public String writeFile(String uniqueId, ByteBuf buf, String extension) { if (path != null) { int size = buf.readableBytes(); String name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "." + extension; try (FileOutputStream output = new FileOutputStream(createFile(uniqueId, name)); FileChannel fileChannel = output.getChannel()) { ByteBuffer byteBuffer = buf.nioBuffer(); int written = 0; while (written < size) { written += fileChannel.write(byteBuffer); } fileChannel.force(false); return name; } catch (IOException e) { LOGGER.warn("Save media file error", e); } } return null; }
private void doPreallocationChunkedZeros(RecoverableRandomAccessFile file) { preAllocateDirectBuffer.limit(preAllocateDirectBuffer.capacity()); preAllocateDirectBuffer.rewind(); try { FileChannel channel = file.getChannel(); int remLen = maxFileLength; while (remLen > 0) { if (remLen < preAllocateDirectBuffer.remaining()) { preAllocateDirectBuffer.limit(remLen); } int writeLen = channel.write(preAllocateDirectBuffer); remLen -= writeLen; preAllocateDirectBuffer.rewind(); } channel.force(false); channel.position(0); } catch (ClosedByInterruptException ignored) { LOG.trace("Could not preallocate journal file with zeros", ignored); } catch (IOException e) { LOG.error("Could not preallocate journal file with zeros! Will continue without preallocation", e); } }
private void complete() throws IOException { StringBuilder buffer = (StringBuilder) TransactionSynchronizationManager.getResource(bufferKey); if (buffer != null) { String string = buffer.toString(); byte[] bytes = string.getBytes(encoding); int bufferLength = bytes.length; ByteBuffer bb = ByteBuffer.wrap(bytes); int bytesWritten = channel.write(bb); if(bytesWritten != bufferLength) { throw new IOException("All bytes to be written were not successfully written"); } if (forceSync) { channel.force(false); } if (TransactionSynchronizationManager.hasResource(closeKey)) { closeCallback.run(); } } }
public boolean renameTo(File dest) throws IOException { if (dest == null) { throw new NullPointerException("dest"); } if (channelBuffer == null) { // empty file dest.createNewFile(); isRenamed = true; return true; } int length = channelBuffer.readableBytes(); FileOutputStream outputStream = new FileOutputStream(dest); FileChannel fileChannel = outputStream.getChannel(); ByteBuffer byteBuffer = channelBuffer.toByteBuffer(); int written = 0; while (written < length) { written += fileChannel.write(byteBuffer); } fileChannel.force(false); fileChannel.close(); outputStream.close(); isRenamed = true; return written == length; }
@Override public void write(FileChannel fileChannel) throws IOException { byteBuffer().position(0); byteBuffer().putShort(magic); byteBuffer().putLong(fileLength); byteBuffer().putInt(totalNum.get()); byteBuffer().putInt(aliveNum.get()); byteBuffer().putInt(isFull); byteBuffer().putLong(storeTxLogRecordId); byteBuffer().flip(); fileChannel.position(0); fileChannel.write(byteBuffer()); fileChannel.force(true); }