java.nio.file.Files.newByteChannel()方法的使用及代码示例

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

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

Files.newByteChannel介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation opens a Channel for the underlying file.
 * @see Files#newByteChannel(Path, OpenOption...)
 */
@Override
public WritableByteChannel writableChannel() throws IOException {
  return Files.newByteChannel(this.path, StandardOpenOption.WRITE);
}

代码示例来源:origin: org.springframework/spring-core

/**
 * This implementation opens a Channel for the underlying file.
 * @see Files#newByteChannel(Path, OpenOption...)
 */
@Override
public WritableByteChannel writableChannel() throws IOException {
  return Files.newByteChannel(this.path, StandardOpenOption.WRITE);
}

代码示例来源:origin: line/armeria

@Override
protected ByteChannel newStream() throws IOException {
  try {
    return Files.newByteChannel(path, StandardOpenOption.READ);
  } catch (NoSuchFileException e) {
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation opens a Channel for the underlying file.
 * @see Files#newByteChannel(Path, OpenOption...)
 */
@Override
public ReadableByteChannel readableChannel() throws IOException {
  try {
    return Files.newByteChannel(this.path, StandardOpenOption.READ);
  }
  catch (NoSuchFileException ex) {
    throw new FileNotFoundException(ex.getMessage());
  }
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * Opens file to write a 7z archive to.
 *
 * @param filename the file to write to
 * @throws IOException if opening the file fails
 */
public SevenZOutputFile(final File filename) throws IOException {
  this(Files.newByteChannel(filename.toPath(),
    EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE,
          StandardOpenOption.TRUNCATE_EXISTING)));
}

代码示例来源:origin: google/guava

@Override
public byte[] read() throws IOException {
 try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
  return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size());
 }
}

代码示例来源:origin: org.springframework/spring-core

/**
 * This implementation opens a Channel for the underlying file.
 * @see Files#newByteChannel(Path, OpenOption...)
 */
@Override
public ReadableByteChannel readableChannel() throws IOException {
  try {
    return Files.newByteChannel(this.path, StandardOpenOption.READ);
  }
  catch (NoSuchFileException ex) {
    throw new FileNotFoundException(ex.getMessage());
  }
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * Reads a file as 7z archive
 *
 * @param filename the file to read
 * @param password optional password if the archive is encrypted
 * @throws IOException if reading the archive fails
 * @since 1.17
 */
public SevenZFile(final File filename, final char[] password) throws IOException {
  this(Files.newByteChannel(filename.toPath(), EnumSet.of(StandardOpenOption.READ)),
     filename.getAbsolutePath(), utf16Decode(password), true);
}

代码示例来源:origin: google/j2objc

@Override
public byte[] read() throws IOException {
 try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
  return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size());
 }
}

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

@Override
public byte[] read() throws IOException {
 try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
  return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size());
 }
}

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

@ExpectWarning("OS_OPEN_STREAM")
void test4(File f, byte b) throws IOException {
  SeekableByteChannel c = Files.newByteChannel(Paths.get(""), StandardOpenOption.APPEND);
  c.position();
}

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

@ExpectWarning("OS_OPEN_STREAM")
void test3(File f, byte b) throws IOException {
  SeekableByteChannel c = Files.newByteChannel(Paths.get(""));
  c.position();
}

代码示例来源:origin: prestodb/presto

@Override
public byte[] read() throws IOException {
 try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
  return com.facebook.presto.jdbc.internal.guava.io.Files.readFile(
    Channels.newInputStream(channel), channel.size());
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Creates an IndexInput for the file with the given name. */
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
 ensureOpen();
 ensureCanRead(name);
 Path path = directory.resolve(name);
 SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ);
 return new SimpleFSIndexInput("SimpleFSIndexInput(path=\"" + path + "\")", channel, context);
}

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

@Test
public void testNewByteChannelRead_notFound() throws Exception {
 Path path = Paths.get(URI.create("gs://bucket/wednesday"));
 thrown.expect(NoSuchFileException.class);
 Files.newByteChannel(path);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readAndWriteByteChannel() throws Exception {
  Path source = Paths.get(
      DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI());
  Flux<DataBuffer> sourceFlux =
      DataBufferUtils
          .readByteChannel(() -> FileChannel.open(source, StandardOpenOption.READ),
              this.bufferFactory, 3);
  Path destination = Files.createTempFile("DataBufferUtilsTests", null);
  WritableByteChannel channel = Files.newByteChannel(destination, StandardOpenOption.WRITE);
  DataBufferUtils.write(sourceFlux, channel)
      .subscribe(DataBufferUtils.releaseConsumer(),
          throwable -> fail(throwable.getMessage()),
          () -> {
            try {
              String expected = String.join("", Files.readAllLines(source));
              String result = String.join("", Files.readAllLines(destination));
              assertEquals(expected, result);
            }
            catch (IOException e) {
              fail(e.getMessage());
            }
            finally {
              DataBufferUtils.closeChannel(channel);
            }
          });
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void writeWritableByteChannelCancel() throws Exception {
  DataBuffer foo = stringBuffer("foo");
  DataBuffer bar = stringBuffer("bar");
  Flux<DataBuffer> flux = Flux.just(foo, bar);
  WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
  Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
  StepVerifier.create(writeResult, 1)
      .consumeNextWith(stringConsumer("foo"))
      .thenCancel()
      .verify(Duration.ofSeconds(5));
  String result = String.join("", Files.readAllLines(tempFile));
  assertEquals("foo", result);
  channel.close();
  flux.subscribe(DataBufferUtils::release);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void writeWritableByteChannel() throws Exception {
  DataBuffer foo = stringBuffer("foo");
  DataBuffer bar = stringBuffer("bar");
  DataBuffer baz = stringBuffer("baz");
  DataBuffer qux = stringBuffer("qux");
  Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux);
  WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
  Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
  verifyWrittenData(writeResult);
  channel.close();
}

代码示例来源:origin: spring-projects/spring-framework

@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();
}

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

@Test
public void testCloseWhilePrefetching() throws Exception {
 SeekableByteChannel chan =
   SeekableByteChannelPrefetcher.addPrefetcher(10, Files.newByteChannel(input));
 // read just 1 byte, get the prefetching going
 ByteBuffer one = ByteBuffer.allocate(1);
 readFully(chan, one);
 // closing must not throw an exception, even if the prefetching
 // thread is active.
 chan.close();
}

相关文章

微信公众号

最新文章

更多