Java Code Examples for java.nio.channels.ReadableByteChannel

The following examples show how to use java.nio.channels.ReadableByteChannel. These examples are extracted from open source projects. 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 Project: neembuu-uploader   Source File: HttpUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static ReadableByteChannel wrap(final InputStream is, final HttpEntity he,
        final Content c, final double contentLength){
    return new ReadableByteChannel() {
        double total = 0;
        @Override public int read(ByteBuffer dst) throws IOException {
            byte[]b=new byte[dst.capacity()];
            int r = is.read(b); 
            //this sleep is just to slow down update to see, if the UI is working or not !
            // NU's update is very very very fast
            //try{Thread.sleep(1000);}catch(Exception a){}
            dst.put(b,0,r); total+=r; c.setProgress(total/contentLength);
            return r;
        }
        @Override public boolean isOpen() {
            return he.isStreaming();
        }
        @Override public void close() throws IOException {
            is.close();
        }
    };
}
 
Example 2
Source Project: netbeans   Source File: RestJSONResponseParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Copy all data from <code>in</code> {@link InputStream}
 * into <code>out</code> {@link OutputStream}.
 * <p/>
 * @param in  Source {@link InputStream} to read all data.
 * @param out Target {@link OutputStream} to write all data.
 * @throws IOException when there is a problem with copying data.
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    try {
        ReadableByteChannel inChannel = Channels.newChannel(in);
        WritableByteChannel outChannel = Channels.newChannel(out);
        ByteBuffer byteBuffer = ByteBuffer.allocate(10240);
        int read;
        do {
            read = inChannel.read(byteBuffer);
            if (read > 0) {
                byteBuffer.limit(byteBuffer.position());
                byteBuffer.rewind();
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } while (read != -1);
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
 
Example 3
Source Project: urltodrive   Source File: NonResumableDriveUploader.java    License: MIT License 6 votes vote down vote up
private void downloadFile() throws IOException {
    String random = generateRandomString();
    File downloadDirectory = new File(System.getenv("TEMP_STROAGE"), random);
    boolean created = downloadDirectory.mkdir();

    if (!created) {
        uploadInformation.setUploadStatus(UploadStatus.failed);
        uploadInformation.setErrorMessage("Can not create download directory.");
        throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, "Can not create download directory.");
    }

    downloadedFile = new File(downloadDirectory, downloadFileInfo.getFileName());
    ReadableByteChannel rbc = Channels.newChannel(downloadFileInfo.getUploadUrl().openStream());
    FileOutputStream fos = new FileOutputStream(downloadedFile);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
}
 
Example 4
public static void main(String[] args) throws IOException {
    ReadableByteChannel channel = new ReadableByteChannel() {
        public int read(ByteBuffer dst) {
            dst.put((byte) 129);
            return 1;
        }

        public boolean isOpen() {
            return true;
        }

        public void close() {
        }
    };

    InputStream in = Channels.newInputStream(channel);
    int data = in.read();
    if (data < 0)
        throw new RuntimeException(
            "InputStream.read() spec'd to return 0-255");
}
 
Example 5
Source Project: yajsync   Source File: ChannelTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testTransferSingleMinInt() throws ChannelException
{
    ByteBuffer wb = ByteBuffer.allocate(8);
    WritableByteBufferChannel w = new WritableByteBufferChannel(wb);
    RsyncOutChannel _out = new RsyncOutChannel(w);

    int testInt = Integer.MIN_VALUE;
    _out.putInt(testInt);
    _out.flush();
    assertTrue(wb.position() > 0);

    wb.flip();
    ReadableByteChannel r = new ReadableByteBufferChannel(wb);
    RsyncInChannel _in = new RsyncInChannel(r, this);

    int resultInt = _in.getInt();
    assertTrue(wb.position() > 0);
    assertTrue(resultInt == testInt);
}
 
Example 6
Source Project: arcusplatform   Source File: MP4Convert.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean fill(ByteBuffer buffer, ReadableByteChannel input) throws IOException {
   buffer.compact();
   while (true) {
      int read = input.read(buffer);
      int size = buffer.position();

      if (size >= PACKET_SIZE) {
         buffer.flip();
         return true;
      }

      if (read < 0) {
         buffer.flip();
         return false;
      }
   }
}
 
Example 7
@NotNull
private static File downloadIndex(@NotNull URL indexUrl) throws IOException {
    LOGGER.info("Downloading index from {}", indexUrl);

    ReadableByteChannel indexChannel = Channels.newChannel(indexUrl.openStream());

    String extension = ".bai";
    if (indexUrl.getPath().contains(".crai")) {
        LOGGER.debug("Located crai from the index on {}", indexUrl);
        extension = ".crai";
    }

    File index = File.createTempFile("tmp", extension);
    FileOutputStream indexOutputStream = new FileOutputStream(index);
    indexOutputStream.getChannel().transferFrom(indexChannel, 0, Long.MAX_VALUE);
    indexOutputStream.close();
    indexChannel.close();

    LOGGER.info("Downloaded index to {}", index.getPath());

    return index;
}
 
Example 8
Source Project: hbase   Source File: MultiByteBuff.java    License: Apache License 2.0 6 votes vote down vote up
private int internalRead(ReadableByteChannel channel, long offset,
    ChannelReader reader) throws IOException {
  checkRefCount();
  int total = 0;
  while (buffsIterator.hasNext()) {
    ByteBuffer buffer = buffsIterator.next();
    int len = read(channel, buffer, offset, reader);
    if (len > 0) {
      total += len;
      offset += len;
    }
    if (buffer.hasRemaining()) {
      break;
    }
  }
  return total;
}
 
Example 9
Source Project: riiablo   Source File: ClientNetworkReceiver.java    License: Apache License 2.0 6 votes vote down vote up
@Override
  protected void processSystem() {
    InputStream in = socket.getInputStream();
    try {
      if (in.available() > 0) {
        ReadableByteChannel channel = Channels.newChannel(in);
        buffer.clear();
        int i = channel.read(buffer);
        buffer.rewind().limit(i);
        D2GS d2gs = new D2GS();
        int p = 0;
        while (buffer.hasRemaining()) {
          int size = ByteBufferUtil.getSizePrefix(buffer);
          D2GS.getRootAsD2GS(ByteBufferUtil.removeSizePrefix(buffer), d2gs);
          if (DEBUG_PACKET) Gdx.app.debug(TAG, p++ + " packet type " + D2GSData.name(d2gs.dataType()) + ":" + size + "B");
          process(d2gs);
//          System.out.println(buffer.position() + "->" + (buffer.position() + size + 4));
          buffer.position(buffer.position() + size + 4); // advance position passed current packet + size prefix of next packet
        }
      }
    } catch (Throwable t) {
      Gdx.app.error(TAG, t.getMessage(), t);
    }
  }
 
Example 10
Source Project: TencentKona-8   Source File: ReadOffset.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    ReadableByteChannel rbc = new ReadableByteChannel() {
        public int read(ByteBuffer dst) {
            dst.put((byte)0);
            return 1;
        }
        public boolean isOpen() {
            return true;
        }
        public void close() {
        }
    };

    InputStream in = Channels.newInputStream(rbc);

    byte[] b = new byte[3];
    in.read(b, 0, 1);
    in.read(b, 2, 1);       // throws IAE
}
 
Example 11
Source Project: AsyncSocket   Source File: Util.java    License: MIT License 6 votes vote down vote up
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();
        // write to the channel, may block
        dest.write(buffer);
        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing recycle()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}
 
Example 12
Source Project: jdk8u-jdk   Source File: ReadByte.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    ReadableByteChannel channel = new ReadableByteChannel() {
        public int read(ByteBuffer dst) {
            dst.put((byte) 129);
            return 1;
        }

        public boolean isOpen() {
            return true;
        }

        public void close() {
        }
    };

    InputStream in = Channels.newInputStream(channel);
    int data = in.read();
    if (data < 0)
        throw new RuntimeException(
            "InputStream.read() spec'd to return 0-255");
}
 
Example 13
Source Project: java-docs-samples   Source File: CsvToAvro.java    License: Apache License 2.0 6 votes vote down vote up
public static String getSchema(String schemaPath) throws IOException {
  ReadableByteChannel chan = FileSystems.open(FileSystems.matchNewResource(
      schemaPath, false));

  try (InputStream stream = Channels.newInputStream(chan)) {
    BufferedReader streamReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    StringBuilder dataBuilder = new StringBuilder();

    String line;
    while ((line = streamReader.readLine()) != null) {
      dataBuilder.append(line);
    }

    return dataBuilder.toString();
  }
}
 
Example 14
@Override
public ReadableByteChannel next() {
	Input input = filenamesIterator.next();
	currentInnerFileName = input.getAbsolutePath();
	
	defaultLogger.debug("Opening input file " + currentInnerFileName);
	try {
		ReadableByteChannel channel = (ReadableByteChannel) input.getPreferredInput(DataSourceType.CHANNEL);
		defaultLogger.debug("Reading input file " + currentInnerFileName);
		return channel;
	} catch (IOException e) {
		throw new JetelRuntimeException("File is unreachable: " + currentInnerFileName, e);
	}
}
 
Example 15
Source Project: spring_io_2019   Source File: MavenWrapperDownloader.java    License: Apache License 2.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 16
Source Project: pulsar   Source File: FunctionCommon.java    License: Apache License 2.0 5 votes vote down vote up
public static void downloadFromHttpUrl(String destPkgUrl, File targetFile) throws IOException {
    URL website = new URL(destPkgUrl);

    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    log.info("Downloading function package from {} to {} ...", destPkgUrl, targetFile.getAbsoluteFile());
    try (FileOutputStream fos = new FileOutputStream(targetFile)) {
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    }
    log.info("Downloading function package from {} to {} completed!", destPkgUrl, targetFile.getAbsoluteFile());
}
 
Example 17
Source Project: beam   Source File: AvroSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void startReading(ReadableByteChannel channel) throws IOException {
  try {
    metadata = readMetadataFromFile(getCurrentSource().getSingleFileMetadata().resourceId());
  } catch (IOException e) {
    throw new RuntimeException(
        "Error reading metadata from file " + getCurrentSource().getSingleFileMetadata(), e);
  }

  long startOffset = getCurrentSource().getStartOffset();
  byte[] syncMarker = metadata.getSyncMarker();
  long syncMarkerLength = syncMarker.length;

  if (startOffset != 0) {
    // Rewind order to find the sync marker ending the previous block.
    long position = Math.max(0, startOffset - syncMarkerLength);
    ((SeekableByteChannel) channel).position(position);
    startOffset = position;
  }

  // Satisfy the post condition.
  stream = createStream(channel);
  countStream = new CountingInputStream(stream);
  synchronized (progressLock) {
    currentBlockOffset = startOffset + advancePastNextSyncMarker(stream, syncMarker);
    currentBlockSizeBytes = 0;
  }
}
 
Example 18
Source Project: canal-1.1.3   Source File: SimpleCanalConnector.java    License: Apache License 2.0 5 votes vote down vote up
private byte[] readNextPacket(ReadableByteChannel channel) throws IOException {
    synchronized (readDataLock) {
        readHeader.clear();
        read(channel, readHeader);
        int bodyLen = readHeader.getInt(0);
        ByteBuffer bodyBuf = ByteBuffer.allocate(bodyLen).order(ByteOrder.BIG_ENDIAN);
        read(channel, bodyBuf);
        return bodyBuf.array();
    }
}
 
Example 19
private long transferFromArbitraryChannel(ReadableByteChannel src,
                                          long position, long count)
    throws IOException
{
    // Untrusted target: Use a newly-erased buffer
    int c = (int)Math.min(count, TRANSFER_SIZE);
    ByteBuffer bb = ByteBuffer.allocate(c);
    long tw = 0;                    // Total bytes written
    long pos = position;
    try {
        while (tw < count) {
            bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
            // ## Bug: Will block reading src if this channel
            // ##      is asynchronously closed
            int nr = src.read(bb);
            if (nr <= 0)
                break;
            bb.flip();
            int nw = write(bb, pos);
            tw += nw;
            if (nw != nr)
                break;
            pos += nw;
            bb.clear();
        }
        return tw;
    } catch (IOException x) {
        if (tw > 0)
            return tw;
        throw x;
    }
}
 
Example 20
Source Project: SpringBootLearn   Source File: MavenWrapperDownloader.java    License: Apache License 2.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 21
Source Project: DataflowTemplates   Source File: BulkCompressor.java    License: Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext context) {
  ResourceId inputFile = context.element().resourceId();
  Compression compression = compressionValue.get();

  // Add the compression extension to the output filename. Example: demo.txt -> demo.txt.gz
  String outputFilename = inputFile.getFilename() + compression.getSuggestedSuffix();

  // Resolve the necessary resources to perform the transfer
  ResourceId outputDir = FileSystems.matchNewResource(destinationLocation.get(), true);
  ResourceId outputFile =
      outputDir.resolve(outputFilename, StandardResolveOptions.RESOLVE_FILE);
  ResourceId tempFile =
      outputDir.resolve("temp-" + outputFilename, StandardResolveOptions.RESOLVE_FILE);

  // Perform the copy of the compressed channel to the destination.
  try (ReadableByteChannel readerChannel = FileSystems.open(inputFile)) {
    try (WritableByteChannel writerChannel =
        compression.writeCompressed(FileSystems.create(tempFile, MimeTypes.BINARY))) {

      // Execute the copy to the temporary file
      ByteStreams.copy(readerChannel, writerChannel);
    }

    // Rename the temporary file to the output file
    FileSystems.rename(ImmutableList.of(tempFile), ImmutableList.of(outputFile));

    // Output the path to the uncompressed file
    context.output(outputFile.toString());
  } catch (IOException e) {
    LOG.error("Error occurred during compression of {}", inputFile.toString(), e);
    context.output(DEADLETTER_TAG, KV.of(inputFile.toString(), e.getMessage()));
  }
}
 
Example 22
Source Project: RxFile   Source File: RxFile.java    License: Apache License 2.0 5 votes vote down vote up
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
    throws IOException {
  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 23
Source Project: tensorboot   Source File: MavenWrapperDownloader.java    License: Apache License 2.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 24
Source Project: SpringBootLearn   Source File: MavenWrapperDownloader.java    License: Apache License 2.0 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 25
Source Project: software-demo   Source File: MavenWrapperDownloader.java    License: MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 26
Source Project: blog-tutorials   Source File: MavenWrapperDownloader.java    License: MIT License 5 votes vote down vote up
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
    URL website = new URL(urlString);
    ReadableByteChannel rbc;
    rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    rbc.close();
}
 
Example 27
Source Project: aifh   Source File: FileUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Download a file from the Internet to a local path.
 * @param url The URL to download from.
 * @param path The path to download to.
 */
public static void downloadFile(String url, File path) {
    try {
        URL website = new URL(url);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream(path);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch(IOException ex) {
        path.delete();// Delete failed file
        throw new AIFHError(ex);
    }
}
 
Example 28
Source Project: tutorials   Source File: FileDownload.java    License: MIT License 5 votes vote down vote up
public static void downloadWithJavaNIO(String fileURL, String localFilename) throws IOException {

        URL url = new URL(fileURL);
        try (ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); 
            FileOutputStream fileOutputStream = new FileOutputStream(localFilename); FileChannel fileChannel = fileOutputStream.getChannel()) {

            fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
            fileOutputStream.close();
        }
    }
 
Example 29
Source Project: gnirehtet   Source File: AdbMonitor.java    License: Apache License 2.0 5 votes vote down vote up
private boolean consumeOkay(ReadableByteChannel channel) throws IOException {
    while (channel.read(socketBuffer) != -1) {
        if (socketBuffer.position() < OKAY_SIZE) {
            // not enough data
            continue;
        }
        socketBuffer.flip();
        socketBuffer.get(BUFFER, 0, OKAY_SIZE);
        socketBuffer.compact();
        socketBuffer.flip();
        String text = new String(BUFFER, 0, OKAY_SIZE, StandardCharsets.US_ASCII);
        return "OKAY".equals(text);
    }
    return false;
}
 
Example 30
Source Project: appengine-gcs-client   Source File: LocalRawGcsService.java    License: Apache License 2.0 5 votes vote down vote up
private Token appendFileContentsToToken(GcsFilename source, Token token) throws IOException {
  ByteBuffer chunk = ByteBuffer.allocate(1024);

  try (ReadableByteChannel readChannel = Channels.newChannel(
      blobStorage.fetchBlob(getBlobKeyForFilename(source)))) {
    while (readChannel.read(chunk) != -1) {
      chunk.flip();
      token = append(token, chunk);
      chunk.clear();
    }
  }
  return token;
}