java.nio.channels.WritableByteChannel Java Examples

The following examples show how to use java.nio.channels.WritableByteChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TFRecordIO.java    From beam with Apache License 2.0 6 votes vote down vote up
public void write(WritableByteChannel outChannel, byte[] data) throws IOException {
  int maskedCrc32OfLength = hashLong(data.length);
  int maskedCrc32OfData = hashBytes(data);

  header.clear();
  header.putLong(data.length).putInt(maskedCrc32OfLength);
  header.rewind();
  writeFully(outChannel, header);

  writeFully(outChannel, ByteBuffer.wrap(data));

  footer.clear();
  footer.putInt(maskedCrc32OfData);
  footer.rewind();
  writeFully(outChannel, footer);
}
 
Example #2
Source File: NIOSender.java    From swift-k with Apache License 2.0 6 votes vote down vote up
private void sendAllEntries(BlockingQueue<NIOSendEntry> q, WritableByteChannel c, SelectionKey key) {
    NIOSendEntry e = null;
    while (true) {
        // get one entry from queue
           synchronized(queues) {
               e = q.peek();
               if (e == null) {
                   queues.remove(c);
                   key.cancel();
                   registered.remove(c);
                   return;
               }
           }
           if (sendAllBuffers(e, c, key)) {
               notifySender(e);
               q.remove();
           }
           else {
               return;
           }
    }
}
 
Example #3
Source File: MessageFormatSend.java    From ambry with Apache License 2.0 6 votes vote down vote up
@Override
public long writeTo(WritableByteChannel channel) throws IOException {
  long written = 0;
  if (!isSendComplete()) {
    written = readSet.writeTo(currentWriteIndex, channel,
        sendInfoList.get(currentWriteIndex).relativeOffset() + sizeWrittenFromCurrentIndex,
        sendInfoList.get(currentWriteIndex).sizetoSend() - sizeWrittenFromCurrentIndex);
    logger.trace("writeindex {} relativeOffset {} maxSize {} written {}", currentWriteIndex,
        sendInfoList.get(currentWriteIndex).relativeOffset() + sizeWrittenFromCurrentIndex,
        sendInfoList.get(currentWriteIndex).sizetoSend() - sizeWrittenFromCurrentIndex, written);
    sizeWritten += written;
    sizeWrittenFromCurrentIndex += written;
    logger.trace("size written in this loop : {} size written till now : {}", written, sizeWritten);
    if (sizeWrittenFromCurrentIndex == sendInfoList.get(currentWriteIndex).sizetoSend()) {
      currentWriteIndex++;
      sizeWrittenFromCurrentIndex = 0;
    }
  }
  return written;
}
 
Example #4
Source File: UDPStreamConsumer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void process(final OutputStream out) throws IOException {
    try {
        long totalBytes = 0L;
        try (WritableByteChannel wbc = Channels.newChannel(new BufferedOutputStream(out))) {
            ByteBuffer buffer = null;
            while ((buffer = filledBuffers.poll(50, TimeUnit.MILLISECONDS)) != null) {
                int bytesWrittenThisPass = 0;
                try {
                    while (buffer.hasRemaining()) {
                        bytesWrittenThisPass += wbc.write(buffer);
                    }
                    totalBytes += bytesWrittenThisPass;
                    if (totalBytes > fileSizeTrigger || flowFilePerDatagram) {
                        break;// this is enough data
                    }
                } finally {
                    bufferPool.returnBuffer(buffer, bytesWrittenThisPass);
                }
            }
        }
    } catch (final InterruptedException ie) {
        // irrelevant
    }
}
 
Example #5
Source File: DataBufferUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeWritableByteChannelErrorInFlux() throws Exception {
	DataBuffer foo = stringBuffer("foo");
	DataBuffer bar = stringBuffer("bar");
	Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException()));

	WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);

	Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
	StepVerifier.create(writeResult)
			.consumeNextWith(stringConsumer("foo"))
			.consumeNextWith(stringConsumer("bar"))
			.expectError()
			.verify(Duration.ofSeconds(5));

	String result = String.join("", Files.readAllLines(tempFile));

	assertEquals("foobar", result);
	channel.close();
}
 
Example #6
Source File: QueryMessageTransfer.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
    if (this.byteBufferHeader.hasRemaining()) {
        transferred += target.write(this.byteBufferHeader);
        return transferred;
    } else {
        List<ByteBuffer> messageBufferList = this.queryMessageResult.getMessageBufferList();
        for (ByteBuffer bb : messageBufferList) {
            if (bb.hasRemaining()) {
                transferred += target.write(bb);
                return transferred;
            }
        }
    }

    return 0;
}
 
Example #7
Source File: QueryMessageTransfer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
    if (this.byteBufferHeader.hasRemaining()) {
        transfered += target.write(this.byteBufferHeader);
        return transfered;
    } else {
        List<ByteBuffer> messageBufferList = this.queryMessageResult.getMessageBufferList();
        for (ByteBuffer bb : messageBufferList) {
            if (bb.hasRemaining()) {
                transfered += target.write(bb);
                return transfered;
            }
        }
    }

    return 0;
}
 
Example #8
Source File: TestUtils.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Helper to generate files for testing.
 *
 * @param filePath The path to the file to write.
 * @param lines The lines to write.
 * @param compression The compression type of the file.
 * @return The file written.
 * @throws IOException If an error occurs while creating or writing the file.
 */
public static ResourceId writeToFile(
    String filePath, List<String> lines, Compression compression) throws IOException {

  String fileContents = String.join(System.lineSeparator(), lines);

  ResourceId resourceId = FileSystems.matchNewResource(filePath, false);

  String mimeType =
      compression == Compression.UNCOMPRESSED ? MimeTypes.TEXT : MimeTypes.BINARY;

  // Write the file contents to the channel and close.
  try (ReadableByteChannel readChannel =
      Channels.newChannel(new ByteArrayInputStream(fileContents.getBytes()))) {
    try (WritableByteChannel writeChannel =
        compression.writeCompressed(FileSystems.create(resourceId, mimeType))) {
      ByteStreams.copy(readChannel, writeChannel);
    }
  }

  return resourceId;
}
 
Example #9
Source File: StreamUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * Read all available bytes from one channel and copy them to the other.
    * Can be more efficient when the target is a {@link FileChannel} 
    * and the size of the source is known.
    * 
    * @param in
    * @param out
    * @param sourceSize the size of the source or 0 when unknown
    * 
    * @throws IOException
    */
public static void copy(ReadableByteChannel in, WritableByteChannel out, long sourceSize) throws IOException {
	if ((out instanceof FileChannel) && (sourceSize > 0)) {
		FileChannel outputFileChannel = (FileChannel) out;
		long pos = 0;
		long transferred = 0;
		long count = 0;
		while (pos < sourceSize) {
			count = Math.min(Defaults.MAX_MAPPED_FILE_TRANSFER_SIZE, sourceSize - pos); // CL-2313
			transferred = outputFileChannel.transferFrom(in, pos, count);
			if (transferred == 0) {
				break;
			}
			pos += transferred;
		}
		if (pos != sourceSize) {
			throw new IOException(String.format("Failed to copy the whole content: expected %d, transferred %d bytes", sourceSize, pos));
		}
	} else {
		copy(in, out);
	}
}
 
Example #10
Source File: ClassloaderByteArraySource.java    From QuickTheories with Apache License 2.0 5 votes vote down vote up
private static void copy(final InputStream input, final OutputStream output)
    throws IOException {
  final ReadableByteChannel src = Channels.newChannel(input);
  final WritableByteChannel dest = Channels.newChannel(output);
  final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
  while (src.read(buffer) != -1) {
    buffer.flip();
    dest.write(buffer);
    buffer.compact();
  }
  buffer.flip();
  while (buffer.hasRemaining()) {
    dest.write(buffer);
  }
}
 
Example #11
Source File: FileSender.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ch Output channel to write file to.
 * @param oo Channel to write meta info to.
 * @param rcvMeta Connection meta received.
 * @throws IOException If a transport exception occurred.
 * @throws InterruptedException If thread interrupted.
 */
public void send(WritableByteChannel ch,
    ObjectOutput oo,
    @Nullable TransmissionMeta rcvMeta
) throws IOException, InterruptedException {
    updateSenderState(rcvMeta);

    // Write flag to remote to keep currnet transmission opened.
    oo.writeBoolean(false);

    // Send meta about current file to remote.
    oo.writeObject(new TransmissionMeta(meta.name(),
        meta.offset() + transferred,
        meta.count() - transferred,
        meta.params(),
        meta.policy(),
        null));

    while (hasNextChunk()) {
        if (Thread.currentThread().isInterrupted())
            throw new InterruptedException("Sender thread has been interruped");

        if (stopped())
            throw new IgniteException("Sender has been cancelled due to the local node is stopping");

        writeChunk(ch);
    }

    assert transferred == meta.count() : "File is not fully transferred [expect=" + meta.count() +
        ", actual=" + transferred + ']';
}
 
Example #12
Source File: DTSTrackImpl.java    From mp4parser with Apache License 2.0 5 votes vote down vote up
private List<Sample> generateSamples(DataSource dataSource, int dataOffset, long dataSize, int corePresent) throws IOException {
    LookAhead la = new LookAhead(dataSource, dataOffset, dataSize, corePresent);
    ByteBuffer sample;
    List<Sample> mySamples = new ArrayList<Sample>();

    while ((sample = la.findNextStart()) != null) {
        final ByteBuffer finalSample = sample;
        mySamples.add(new Sample() {
            public void writeTo(WritableByteChannel channel) throws IOException {
                channel.write((ByteBuffer) ((Buffer)finalSample).rewind());
            }

            public long getSize() {
                return ((Buffer)finalSample).rewind().remaining();
            }

            public ByteBuffer asByteBuffer() {
                return finalSample;
            }

            @Override
            public SampleEntry getSampleEntry() {
                return audioSampleEntry;
            }
        });
        //System.err.println(finalSample.remaining());
    }
    System.err.println("all samples found");
    return mySamples;
}
 
Example #13
Source File: Germ.java    From swim with Apache License 2.0 5 votes vote down vote up
public void writeValue(WritableByteChannel channel) {
  final ByteBuffer buffer = toByteBuffer();
  int k;
  try {
    do {
      k = channel.write(buffer);
    } while (k > 0 && buffer.hasRemaining());
    if (buffer.hasRemaining()) {
      throw new StoreException("wrote incomplete germ");
    }
  } catch (IOException cause) {
    throw new StoreException(cause);
  }
}
 
Example #14
Source File: ChannelsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNewWriterWritableByteChannelString() throws IOException {
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
    Writer testWriter = Channels.newWriter(wbChannel, CODE_SET); //$NON-NLS-1$
    Writer testWriter_s = Channels.newWriter(wbChannel, Charset.forName(
            CODE_SET).newEncoder(), //$NON-NLS-1$
            -1);

    String writebuf = ""; //$NON-NLS-1$
    for (int val = 0; val < this.writebufSize / 2; val++) {
        writebuf = writebuf + ((char) (val + 64));
    }
    byte[] bit = new byte[1];
    bit[0] = 80;
    this.fouts.write(bit);
    this.assertFileSizeSame(tmpFile, 1);

    // writer continues to write after '1',what the fouts write
    testWriter.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
    // testwriter_s does not know if testwrite writes
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
    // testwriter_s even does not know if himself writes?
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);

    // close the fouts, no longer writable for testWriter
    for (int val = 0; val < this.writebufSize; val++) {
        writebuf = writebuf + ((char) (val + 64));
    }
    this.fouts.close();
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
}
 
Example #15
Source File: ChannelHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Channel copy method 1. This method copies data from the src channel and
 * writes it to the dest channel until EOF on src. This implementation makes
 * use of compact( ) on the temp buffer to pack down the data if the buffer
 * wasn't fully drained. This may result in data copying, but minimizes system
 * calls. It also requires a cleanup loop to make sure all the data gets sent.
 * <br>
 * Source: Java NIO, page 60
 *
 * @param aSrc
 *        Source channel. May not be <code>null</code>. Is not closed after
 *        the operation.
 * @param aDest
 *        Destination channel. May not be <code>null</code>. Is not closed
 *        after the operation.
 * @return The number of bytes written.
 */
@Nonnegative
private static long _channelCopy1 (@Nonnull @WillNotClose final ReadableByteChannel aSrc,
                                   @Nonnull @WillNotClose final WritableByteChannel aDest) throws IOException
{
  long nBytesWritten = 0;
  final ByteBuffer aBuffer = ByteBuffer.allocateDirect (16 * 1024);
  while (aSrc.read (aBuffer) != -1)
  {
    // Prepare the buffer to be drained
    aBuffer.flip ();

    // Write to the channel; may block
    nBytesWritten += aDest.write (aBuffer);

    // If partial transfer, shift remainder down
    // If buffer is empty, same as doing clear()
    aBuffer.compact ();
  }

  // EOF will leave buffer in fill state
  aBuffer.flip ();

  // Make sure that the buffer is fully drained
  while (aBuffer.hasRemaining ())
    nBytesWritten += aDest.write (aBuffer);

  return nBytesWritten;
}
 
Example #16
Source File: ShardingWritableByteChannel.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isOpen() {
  for (WritableByteChannel writer : writers) {
    if (!writer.isOpen()) {
      return false;
    }
  }

  return true;
}
 
Example #17
Source File: DymolaAdaptersMatParamsWriter.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
private void writeMLArrayToPath(Path fPath, List<MLArray> mlarray) throws IOException {
    try (OutputStream w = Files.newOutputStream(fPath)) {
        //using Writable channel to make it work both for 'standard' and shrinkwrap based filesystems
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel wbc = Channels.newChannel(baos);
        new MatFileWriter(wbc, mlarray);
        w.write(baos.toByteArray());
    }
}
 
Example #18
Source File: NTv2Test.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a sub-grid of the given grid in pseudo-NTv2 format. This method is used only for creating the test file.
 * The file created by this method is not fully NTv2 compliant (in particular, we do not write complete header),
 * but we take this opportunity for testing {@code NTv2.Loader} capability to be lenient.
 *
 * <p>This method has been executed once for creating the {@code "NTF_R93-extract.gsb"} test file and should not
 * be needed anymore, but we keep it around in case we have new test files to generate. The parameter used for
 * creating the test file are:</p>
 *
 * <ul>
 *   <li>{@code gridX} = 72</li>
 *   <li>{@code gridY} = 74</li>
 *   <li>{@code nx}    =  6</li>
 *   <li>{@code ny}    =  7</li>
 * </ul>
 *
 * This ensure that the grid indices (75.7432814, 78.4451225) is included in the test file.
 * Those grid indices is the location of the (2°25′32.4187″N 48°50′40.2441″W) test point to interpolate.
 *
 * <h4>Limitations</h4>
 * This method assumes that bounding box and increments have integer values, and that any fractional part
 * is rounding errors. This is usually the case when using the {@code "SECONDS"} unit of measurement.
 * This assumption does not apply to the shift values.
 *
 * @param  grid   the full grid from which to extract a few values.
 * @param  out    where to write the test file.
 * @param  gridX  index along the longitude axis of the first cell to write.
 * @param  gridY  index along the latitude axis of the first cell to write.
 * @param  nx     number of cells to write along the longitude axis.
 * @param  ny     number of cells to write along the latitude axis.
 * @throws TransformException if an error occurred while computing the envelope.
 * @throws IOException if an error occurred while writing the test file.
 */
public static void writeSubGrid(final DatumShiftGridFile<Angle,Angle> grid, final Path out,
        final int gridX, final int gridY, final int nx, final int ny) throws IOException, TransformException
{
    Envelope envelope = new Envelope2D(null, gridX, gridY, nx - 1, ny - 1);
    envelope = Envelopes.transform(grid.getCoordinateToGrid().inverse(), envelope);
    final ByteBuffer buffer = ByteBuffer.allocate(4096);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    writeString(buffer, "NUM_OREC"); buffer.putInt(5); nextRecord(buffer);
    writeString(buffer, "NUM_SREC"); buffer.putInt(7); nextRecord(buffer);
    writeString(buffer, "NUM_FILE"); buffer.putInt(1); nextRecord(buffer);
    writeString(buffer, "GS_TYPE");  writeString(buffer, "SECONDS");
    writeString(buffer, "VERSION");  writeString(buffer, "SIS_TEST");   // Last overview record.
    writeString(buffer, "S_LAT");    buffer.putDouble(StrictMath.rint( envelope.getMinimum(1)));
    writeString(buffer, "N_LAT");    buffer.putDouble(StrictMath.rint( envelope.getMaximum(1)));
    writeString(buffer, "E_LONG");   buffer.putDouble(StrictMath.rint(-envelope.getMaximum(0)));  // Sign reversed.
    writeString(buffer, "W_LONG");   buffer.putDouble(StrictMath.rint(-envelope.getMinimum(0)));
    writeString(buffer, "LAT_INC");  buffer.putDouble(StrictMath.rint( envelope.getSpan(1) / (ny - 1)));
    writeString(buffer, "LONG_INC"); buffer.putDouble(StrictMath.rint( envelope.getSpan(0) / (nx - 1)));
    writeString(buffer, "GS_COUNT"); buffer.putInt(nx * ny); nextRecord(buffer);
    for (int y=0; y<ny; y++) {
        for (int x=0; x<nx; x++) {
            buffer.putFloat((float) grid.getCellValue(1, gridX + x, gridY + y));
            buffer.putFloat((float) grid.getCellValue(0, gridX + x, gridY + y));
            buffer.putFloat(ACCURACY);
            buffer.putFloat(ACCURACY);
        }
    }
    writeString(buffer, "END");
    nextRecord(buffer);
    try (WritableByteChannel c = Files.newByteChannel(out, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
        buffer.flip();
        c.write(buffer);
    }
}
 
Example #19
Source File: UnsafeHolder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
public static OutputStreamChannel newChannelBufferOutputStream(
    WritableByteChannel channel, int bufferSize) throws IOException {
  return (directByteBufferAddressMethod != null
      ? new ChannelBufferUnsafeOutputStream(channel, bufferSize)
      : new ChannelBufferOutputStream(channel, bufferSize));
}
 
Example #20
Source File: ReplicaMetadataRequest.java    From ambry with Apache License 2.0 5 votes vote down vote up
@Override
public long writeTo(WritableByteChannel channel) throws IOException {
  if (bufferToSend == null) {
    bufferToSend = ByteBuffer.allocate((int) sizeInBytes());
    writeHeader();
    bufferToSend.putInt(replicaMetadataRequestInfoList.size());
    for (ReplicaMetadataRequestInfo replicaMetadataRequestInfo : replicaMetadataRequestInfoList) {
      replicaMetadataRequestInfo.writeTo(bufferToSend);
    }
    bufferToSend.putLong(maxTotalSizeOfEntriesInBytes);
    bufferToSend.flip();
  }
  return bufferToSend.remaining() > 0 ? channel.write(bufferToSend) : 0;
}
 
Example #21
Source File: DatagramBufferTest.java    From gnirehtet with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircular() throws IOException {
    ByteBuffer datagram5 = createDatagram(5);
    ByteBuffer datagram3 = createDatagram(3);

    DatagramBuffer datagramBuffer = new DatagramBuffer(14);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    WritableByteChannel channel = Channels.newChannel(bos);

    // write and consume 10 bytes
    datagramBuffer.readFrom(createDatagram(10));
    datagramBuffer.writeTo(Channels.newChannel(new ByteArrayOutputStream())); // forget

    // DatagramBuffer is expected to store the whole datagram (even if it exceeds its "capacity")
    datagramBuffer.readFrom(datagram5);
    datagramBuffer.readFrom(datagram3);

    datagramBuffer.writeTo(channel);
    byte[] result = bos.toByteArray();
    Assert.assertArrayEquals(datagram5.array(), result);

    bos.reset();

    datagramBuffer.writeTo(channel);
    result = bos.toByteArray();
    Assert.assertArrayEquals(datagram3.array(), result);
}
 
Example #22
Source File: CodepointIteratorReadableByteChannel.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static ByteBuffer _convert (@Nonnull @WillClose final ReadableByteChannel aChannel) throws IOException
{
  try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream ();
      final WritableByteChannel aOutChannel = Channels.newChannel (aBAOS))
  {
    final ByteBuffer buf = ByteBuffer.allocate (1024);
    while (aChannel.read (buf) > 0)
    {
      buf.flip ();
      aOutChannel.write (buf);
    }
    return ByteBuffer.wrap (aBAOS.toByteArray ());
  }
}
 
Example #23
Source File: Generator.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
public Builder(WritableByteChannel out, byte[] checksumSeed)
{
    assert out != null;
    assert checksumSeed != null;
    _out = out;
    _checksumSeed = checksumSeed;
}
 
Example #24
Source File: BaseWritableMemoryImpl.java    From incubator-datasketches-memory with Apache License 2.0 5 votes vote down vote up
private void writeToWithExtraCopy(long offsetBytes, long lengthBytes,
    final WritableByteChannel out) throws IOException {
  // Keep the bufLen a multiple of 8, to maybe allow getByteArray() to go a faster path.
  final int bufLen = Ints.checkedCast(Math.max(8, Math.min((getCapacity() / 1024) & ~7L, 4096)));
  final byte[] buf = new byte[bufLen];
  final ByteBuffer bufToWrite = ByteBuffer.wrap(buf);
  while (lengthBytes > 0) {
    final int chunk = (int) Math.min(buf.length, lengthBytes);
    getByteArray(offsetBytes, buf, 0, chunk);
    bufToWrite.clear().limit(chunk);
    writeFully(bufToWrite, out);
    offsetBytes += chunk;
    lengthBytes -= chunk;
  }
}
 
Example #25
Source File: FileChannelImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private long transferToDirectlyInternal(long position, int icount,
                                        WritableByteChannel target,
                                        FileDescriptor targetFD)
    throws IOException
{
    assert !nd.transferToDirectlyNeedsPositionLock() ||
           Thread.holdsLock(positionLock);

    long n = -1;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return -1;
        do {
            n = transferTo0(fd, position, icount, targetFD);
        } while ((n == IOStatus.INTERRUPTED) && isOpen());
        if (n == IOStatus.UNSUPPORTED_CASE) {
            if (target instanceof SinkChannelImpl)
                pipeSupported = false;
            if (target instanceof FileChannelImpl)
                fileSupported = false;
            return IOStatus.UNSUPPORTED_CASE;
        }
        if (n == IOStatus.UNSUPPORTED) {
            // Don't bother trying again
            transferSupported = false;
            return IOStatus.UNSUPPORTED;
        }
        return IOStatus.normalize(n);
    } finally {
        threads.remove(ti);
        end (n > -1);
    }
}
 
Example #26
Source File: SuccinctBuffer.java    From succinct with Apache License 2.0 5 votes vote down vote up
/**
 * Write Succinct data structures to a DataOutputStream.
 *
 * @param os Output stream to write data to.
 * @throws IOException
 */
public void writeToStream(DataOutputStream os) throws IOException {
  WritableByteChannel dataChannel = Channels.newChannel(os);

  os.writeInt(getOriginalSize());
  os.writeInt(getSamplingRateSA());
  os.writeInt(getSamplingRateISA());
  os.writeInt(getSamplingRateNPA());
  os.writeInt(getSampleBitWidth());
  os.writeInt(getAlphabetSize());

  for (int i = 0; i < getAlphabetSize(); i++) {
    os.writeInt(alphabet[i]);
  }

  for (int i = 0; i < sa.limit(); i++) {
    os.writeLong(sa.get(i));
  }

  for (int i = 0; i < isa.limit(); i++) {
    os.writeLong(isa.get(i));
  }

  for (int i = 0; i < columnoffsets.limit(); i++) {
    os.writeInt(columnoffsets.get(i));
  }

  for (int i = 0; i < columns.length; i++) {
    os.writeInt(columns[i].limit());
    dataChannel.write(columns[i].order(ByteOrder.BIG_ENDIAN));
    columns[i].rewind();
  }
}
 
Example #27
Source File: ImmutableRoaringArray.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize.
 *
 * The current bitmap is not modified.
 *
 * @param out the DataOutput stream
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Override
public void serialize(DataOutput out) throws IOException {
  if (buffer.hasArray()) {
    out.write(buffer.array(), buffer.arrayOffset(), buffer.limit());
  } else {
    ByteBuffer tmp = buffer.duplicate();
    tmp.position(0);
    try (WritableByteChannel channel = Channels.newChannel((OutputStream) out)) {
      channel.write(tmp);
    }
  }
}
 
Example #28
Source File: ChannelBufferUnsafeFramedOutputStream.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public ChannelBufferUnsafeFramedOutputStream(WritableByteChannel channel,
    int bufferSize) throws IOException {
  super(channel, bufferSize);
  // position the buffer to skip the length of frame at the start
  this.addrPosition += 4;
  this.doWriteFrameSize = true;
}
 
Example #29
Source File: FileChannelUtil.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
public static void copyAndClose(InputStream input, OutputStream output) throws IOException {
    ReadableByteChannel srcChannel = Channels.newChannel(input);

    WritableByteChannel destChannel = Channels.newChannel(output);
    try {
        copy(srcChannel, destChannel);
    }

    finally {
        srcChannel.close();
        destChannel.close();
    }

}
 
Example #30
Source File: Sender.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
public static Builder newServer(ReadableByteChannel in,
                                WritableByteChannel out,
                                Iterable<Path> sourceFiles,
                                byte[] checksumSeed)
{
    Builder builder = new Builder(in, out, sourceFiles, checksumSeed);
    builder._isSendStatistics = true;
    builder._isExitEarlyIfEmptyList = true;
    builder._isExitAfterEOF = false;
    return builder;
}