java.nio.channels.Channels.newChannel()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(396)

本文整理了Java中java.nio.channels.Channels.newChannel()方法的一些代码示例,展示了Channels.newChannel()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Channels.newChannel()方法的具体详情如下:
包路径:java.nio.channels.Channels
类名称:Channels
方法名:newChannel

Channels.newChannel介绍

[英]Returns a readable channel on the given input stream. The resulting channel has the following properties:

  • If the channel is closed, then the underlying stream is closed as well.
  • It is not buffered.
    [中]返回给定输入流上的可读通道。生成的通道具有以下属性:
    *如果通道已关闭,则基础流也将关闭。
    *它没有缓冲。

代码示例

代码示例来源:origin: stackoverflow.com

URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

代码示例来源:origin: stackoverflow.com

public void writeBuffer(ByteBuffer buffer, OutputStream stream) {
  WritableByteChannel channel = Channels.newChannel(stream);

  channel.write(buffer);
}

代码示例来源:origin: oblac/jodd

/**
   * Downloads resource to a file, potentially very efficiently.
   */
  public static void downloadFile(final String url, final File file) throws IOException {
    try (
      InputStream inputStream = new URL(url).openStream();
      ReadableByteChannel rbc = Channels.newChannel(inputStream);
      FileChannel fileChannel = FileChannel.open(
        file.toPath(),
        StandardOpenOption.CREATE,
        StandardOpenOption.TRUNCATE_EXISTING,
        StandardOpenOption.WRITE)
    ) {
      fileChannel.transferFrom(rbc, 0, Long.MAX_VALUE);
    }
  }
}

代码示例来源:origin: spotbugs/spotbugs

@NoWarning("OBL_UNSATISFIED_OBLIGATION")
public static void transfer(InputStream src, File dest) throws IOException {
  ReadableByteChannel in = Channels.newChannel(src);
  try {
    WritableByteChannel out = new FileOutputStream(dest).getChannel();
    try {
      nioCopy(in, out);
    } finally {
      out.close();
    }
  } finally {
    in.close();
  }
}

代码示例来源:origin: alibaba/mdrill

public static void downloadFromMaster(Map conf, String file,
    String localFile) throws IOException, TException {
  NimbusClient client = NimbusClient.getConfiguredClient(conf);
  String id = client.getClient().beginFileDownload(file);
  WritableByteChannel out = Channels.newChannel(new FileOutputStream(
      localFile));
  while (true) {
    ByteBuffer chunk = client.getClient().downloadChunk(id);
    int written = out.write(chunk);
    if (written == 0)
      break;
  }
  out.close();
}

代码示例来源:origin: stackoverflow.com

rbc = new RBCWrapper( Channels.newChannel( url.openStream() ), contentLength( url ), this );
  fos = new FileOutputStream( localPath );
  fos.getChannel().transferFrom( rbc, 0, Long.MAX_VALUE );
} catch ( Exception e ) {
  System.err.println( "Uh oh: " + e.getMessage() );
double                  progress;
if ( ( n = rbc.read( bb ) ) > 0 ) {
  readSoFar += n;
  progress = expectedSize > 0 ? (double) readSoFar / (double) expectedSize * 100.0 : -1.0;

代码示例来源:origin: atilika/kuromoji

public static ByteBuffer read(InputStream input) throws IOException {
  DataInputStream dataInput = new DataInputStream(input);
  int size = dataInput.readInt();
  ByteBuffer buffer = ByteBuffer.allocate(size);
  ReadableByteChannel channel = Channels.newChannel(dataInput);
  while (buffer.hasRemaining()) {
    channel.read(buffer);
  }
  buffer.rewind();
  return buffer;
}

代码示例来源:origin: apache/hbase

private void processUnwrappedData(byte[] inBuf) throws IOException, InterruptedException {
 ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(inBuf));
    continue; // ping message
   unwrappedData = ByteBuffer.allocate(unwrappedDataLength);

代码示例来源:origin: pentaho/mondrian

ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
WritableByteChannel wch = Channels.newChannel(outputStream);
ReadableByteChannel rch;
for (Object byteChunk : byteChunks) {
    continue;
  rch = Channels.newChannel(
    new ByteArrayInputStream((byte[]) byteChunk));
  do {
    buffer.clear();
    readSize = rch.read(buffer);
    buffer.flip();
    while ((writeSize += wch.write(buffer)) < readSize) {

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Downloads this blob to the given file path using specified blob read options.
 *
 * @param path destination
 * @param options blob read options
 * @throws StorageException upon failure
 */
public void downloadTo(Path path, BlobSourceOption... options) {
 try (OutputStream outputStream = Files.newOutputStream(path);
   ReadChannel reader = reader(options)) {
  WritableByteChannel channel = Channels.newChannel(outputStream);
  ByteBuffer bytes = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
  while (reader.read(bytes) > 0) {
   bytes.flip();
   channel.write(bytes);
   bytes.clear();
  }
 } catch (IOException e) {
  throw new StorageException(e);
 }
}

代码示例来源:origin: stackoverflow.com

public static long stream(InputStream input, OutputStream output) throws IOException {
  try (
    ReadableByteChannel inputChannel = Channels.newChannel(input);
    WritableByteChannel outputChannel = Channels.newChannel(output);
  ) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
    long size = 0;

    while (inputChannel.read(buffer) != -1) {
      buffer.flip();
      size += outputChannel.write(buffer);
      buffer.clear();
    }

    return size;
  }
}

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
  if (blob == null)
    return null;
  try(InputStream is = blob.getBinaryStream()) {
    ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
    ReadableByteChannel readableByteChannel = Channels.newChannel(is);
    readableByteChannel.read(direct);
    Buffer byteBuffer = (Buffer) direct;
    byteBuffer.rewind();
    return BinarySerde.toArray(direct);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: plutext/docx4j

ReadableByteChannel ch = Channels.newChannel(in);
  read = ch.read(b);

代码示例来源:origin: redisson/redisson

/**
   * Downloads resource to a file, potentially very efficiently.
   */
  public static void downloadFile(String url, File file) throws IOException {
    InputStream inputStream = new URL(url).openStream();
    ReadableByteChannel rbc = Channels.newChannel(inputStream);
    FileOutputStream fos = new FileOutputStream(file);
    fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  }
}

代码示例来源:origin: alibaba/mdrill

/** 
 * ¼ҪjarύʹÿԲοStormSubmittersubmitJar
   */
@Override
public String beginFileUpload() throws TException {
  String fileLoc = StormConfig.masterInbox(conf) + "/stormjar-"+ UUID.randomUUID() + ".jar";
  try {
    data.getUploaders().put(fileLoc,Channels.newChannel(new FileOutputStream(fileLoc)));
    LOG.info("Uploading file from client to " + fileLoc);
  } catch (FileNotFoundException e) {
    LOG.error(" file not found " + fileLoc);
  }
  return fileLoc;
}

代码示例来源:origin: alibaba/jstorm

public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException {
  WritableByteChannel out = null;
  NimbusClient client = null;
  try {
    client = NimbusClient.getConfiguredClient(conf, 10 * 1000);
    String id = client.getClient().beginFileDownload(file);
    out = Channels.newChannel(new FileOutputStream(localFile));
    while (true) {
      ByteBuffer chunk = client.getClient().downloadChunk(id);
      int written = out.write(chunk);
      if (written == 0) {
        client.getClient().finishFileDownload(id);
        break;
      }
    }
  } finally {
    if (out != null)
      out.close();
    if (client != null)
      client.close();
  }
}

代码示例来源:origin: apache/tika

inputChannel = Channels.newChannel(input);
ByteBuffer buf = ByteBuffer.allocate(1024 * 5);
int bytesRead = inputChannel.read(buf); // read into buffer.
  bytesRead = inputChannel.read(buf);

代码示例来源:origin: org.apache.hadoop/hadoop-common

ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(
  inBuf));
  unwrappedDataLengthBuffer.flip();
  int unwrappedDataLength = unwrappedDataLengthBuffer.getInt();
  unwrappedData = ByteBuffer.allocate(unwrappedDataLength);

代码示例来源:origin: stackoverflow.com

private long stream(InputStream input, OutputStream output) throws IOException {

  try (ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output)) {
    ByteBuffer buffer = ByteBuffer.allocate(10240);
    long size = 0;

    while (inputChannel.read(buffer) != -1) {
      buffer.flip();
      size += outputChannel.write(buffer);
      buffer.clear();
    }
    return size;
  }
}

代码示例来源:origin: atilika/kuromoji

public static void writeArray(OutputStream output, int[] array) throws IOException {
  DataOutputStream dataOutput = new DataOutputStream(output);
  int length = array.length;
  dataOutput.writeInt(length);
  ByteBuffer tmpBuffer = ByteBuffer.allocate(length * INT_BYTES);
  IntBuffer intBuffer = tmpBuffer.asIntBuffer();
  tmpBuffer.rewind();
  intBuffer.put(array);
  WritableByteChannel channel = Channels.newChannel(dataOutput);
  channel.write(tmpBuffer);
}

相关文章