/** * Writes part of a byte array to a {@link java.nio.channels.WritableByteChannel} * * @param source byte array to copy from * @param channel target WritableByteChannel */ public static void writeToChannel(byte[] source, WritableByteChannel channel) throws IOException { writeToChannel(source, 0, source.length, channel); }
public static void write(ChannelFactory factory, Path checkpointFile, Checkpoint checkpoint, OpenOption... options) throws IOException { final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(V3_FILE_SIZE) { @Override public synchronized byte[] toByteArray() { // don't clone return buf; } }; final String resourceDesc = "checkpoint(path=\"" + checkpointFile + "\", gen=" + checkpoint + ")"; try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, checkpointFile.toString(), byteOutputStream, V3_FILE_SIZE)) { CodecUtil.writeHeader(indexOutput, CHECKPOINT_CODEC, CURRENT_VERSION); checkpoint.write(indexOutput); CodecUtil.writeFooter(indexOutput); assert indexOutput.getFilePointer() == V3_FILE_SIZE : "get you numbers straight; bytes written: " + indexOutput.getFilePointer() + ", buffer size: " + V3_FILE_SIZE; assert indexOutput.getFilePointer() < 512 : "checkpoint files have to be smaller than 512 bytes for atomic writes; size: " + indexOutput.getFilePointer(); } // now go and write to the channel, in one go. try (FileChannel channel = factory.open(checkpointFile, options)) { Channels.writeToChannel(byteOutputStream.toByteArray(), channel); // no need to force metadata, file size stays the same and we did the full fsync // when we first created the file, so the directory entry doesn't change as well channel.force(false); } }
/** * Writes part of a byte array to a {@link java.nio.channels.WritableByteChannel} * * @param source byte array to copy from * @param channel target WritableByteChannel */ public static void writeToChannel(byte[] source, WritableByteChannel channel) throws IOException { writeToChannel(source, 0, source.length, channel); }
/** * Writes part of a byte array to a {@link java.nio.channels.WritableByteChannel} * * @param source byte array to copy from * @param channel target WritableByteChannel */ public static void writeToChannel(byte[] source, WritableByteChannel channel) throws IOException { writeToChannel(source, 0, source.length, channel); }
/** * Writes part of a byte array to a {@link java.nio.channels.WritableByteChannel} * * @param source byte array to copy from * @param channel target WritableByteChannel */ public static void writeToChannel(byte[] source, WritableByteChannel channel) throws IOException { writeToChannel(source, 0, source.length, channel); }
/** * Writes part of a byte array to a {@link java.nio.channels.WritableByteChannel} * * @param source byte array to copy from * @param channel target WritableByteChannel */ public static void writeToChannel(byte[] source, WritableByteChannel channel) throws IOException { writeToChannel(source, 0, source.length, channel); }
@Override public void writeTo(GatheringByteChannel channel) throws IOException { Channels.writeToChannel(bytes, offset, length(), channel); }
@Override public void writeTo(GatheringByteChannel channel) throws IOException { Channels.writeToChannel(buffer, buffer.readerIndex(), length(), channel); }
private void write(FileChannel channel) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; final ByteArrayDataOutput out = new ByteArrayDataOutput(buffer); write(out); Channels.writeToChannel(buffer, channel); }
@Override public void writeTo(GatheringByteChannel channel) throws IOException { // nothing to do if (length == 0) { return; } int currentLength = length; int currentOffset = offset; BytesRef ref = new BytesRef(); while (currentLength > 0) { // try to align to the underlying pages while writing, so no new arrays will be created. int fragmentSize = Math.min(currentLength, PAGE_SIZE - (currentOffset % PAGE_SIZE)); boolean newArray = bytearray.get(currentOffset, fragmentSize, ref); assert !newArray : "PagedBytesReference failed to align with underlying bytearray. offset [" + currentOffset + "], size [" + fragmentSize + "]"; Channels.writeToChannel(ref.bytes, ref.offset, ref.length, channel); currentLength -= ref.length; currentOffset += ref.length; } assert currentLength == 0; }
protected final void flush() throws IOException { assert writeLock.isHeldByCurrentThread(); if (bufferCount > 0) { ensureOpen(); // we use the channel to write, since on windows, writing to the RAF might not be reflected // when reading through the channel final int bufferSize = bufferCount; try { Channels.writeToChannel(buffer, 0, bufferSize, channel); } catch (Throwable ex) { closeWithTragicEvent(ex); throw ex; } writtenOffset += bufferSize; bufferCount = 0; } }
public static void write(ChannelFactory factory, Path checkpointFile, Checkpoint checkpoint, OpenOption... options) throws IOException { final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(FILE_SIZE) { @Override public synchronized byte[] toByteArray() { // don't clone return buf; } }; final String resourceDesc = "checkpoint(path=\"" + checkpointFile + "\", gen=" + checkpoint + ")"; try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, checkpointFile.toString(), byteOutputStream, FILE_SIZE)) { CodecUtil.writeHeader(indexOutput, CHECKPOINT_CODEC, INITIAL_VERSION); checkpoint.write(indexOutput); CodecUtil.writeFooter(indexOutput); assert indexOutput.getFilePointer() == FILE_SIZE : "get you number straights. Bytes written: " + indexOutput.getFilePointer() + " buffer size: " + FILE_SIZE; assert indexOutput.getFilePointer() < 512 : "checkpoint files have to be smaller 512b for atomic writes. size: " + indexOutput.getFilePointer(); } // now go and write to the channel, in one go. try (FileChannel channel = factory.open(checkpointFile, options)) { Channels.writeToChannel(byteOutputStream.toByteArray(), channel); // no need to force metadata, file size stays the same and we did the full fsync // when we first created the file, so the directory entry doesn't change as well channel.force(false); } }
public static void write(ChannelFactory factory, Path checkpointFile, Checkpoint checkpoint, OpenOption... options) throws IOException { final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(V3_FILE_SIZE) { @Override public synchronized byte[] toByteArray() { // don't clone return buf; } }; final String resourceDesc = "checkpoint(path=\"" + checkpointFile + "\", gen=" + checkpoint + ")"; try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, checkpointFile.toString(), byteOutputStream, V3_FILE_SIZE)) { CodecUtil.writeHeader(indexOutput, CHECKPOINT_CODEC, CURRENT_VERSION); checkpoint.write(indexOutput); CodecUtil.writeFooter(indexOutput); assert indexOutput.getFilePointer() == V3_FILE_SIZE : "get you numbers straight; bytes written: " + indexOutput.getFilePointer() + ", buffer size: " + V3_FILE_SIZE; assert indexOutput.getFilePointer() < 512 : "checkpoint files have to be smaller than 512 bytes for atomic writes; size: " + indexOutput.getFilePointer(); } // now go and write to the channel, in one go. try (FileChannel channel = factory.open(checkpointFile, options)) { Channels.writeToChannel(byteOutputStream.toByteArray(), channel); // no need to force metadata, file size stays the same and we did the full fsync // when we first created the file, so the directory entry doesn't change as well channel.force(false); } }
public static void write(ChannelFactory factory, Path checkpointFile, Checkpoint checkpoint, OpenOption... options) throws IOException { final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(V3_FILE_SIZE) { @Override public synchronized byte[] toByteArray() { // don't clone return buf; } }; final String resourceDesc = "checkpoint(path=\"" + checkpointFile + "\", gen=" + checkpoint + ")"; try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, checkpointFile.toString(), byteOutputStream, V3_FILE_SIZE)) { CodecUtil.writeHeader(indexOutput, CHECKPOINT_CODEC, CURRENT_VERSION); checkpoint.write(indexOutput); CodecUtil.writeFooter(indexOutput); assert indexOutput.getFilePointer() == V3_FILE_SIZE : "get you numbers straight; bytes written: " + indexOutput.getFilePointer() + ", buffer size: " + V3_FILE_SIZE; assert indexOutput.getFilePointer() < 512 : "checkpoint files have to be smaller than 512 bytes for atomic writes; size: " + indexOutput.getFilePointer(); } // now go and write to the channel, in one go. try (FileChannel channel = factory.open(checkpointFile, options)) { Channels.writeToChannel(byteOutputStream.toByteArray(), channel); // no need to force metadata, file size stays the same and we did the full fsync // when we first created the file, so the directory entry doesn't change as well channel.force(false); } }