java.nio.channels.SeekableByteChannel Java Examples

The following examples show how to use java.nio.channels.SeekableByteChannel. 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: ReplayCacheTestProc.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static int csize(int p) throws Exception {
    try (SeekableByteChannel chan = Files.newByteChannel(
            Paths.get(dfl(p)), StandardOpenOption.READ)) {
        chan.position(6);
        int cc = 0;
        while (true) {
            try {
                if (AuthTime.readFrom(chan) != null) cc++;
            } catch (BufferUnderflowException e) {
                break;
            }
        }
        return cc;
    } catch (IOException ioe) {
        return 0;
    }
}
 
Example #2
Source File: ReplayCacheTestProc.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static int csize(int p) throws Exception {
    try (SeekableByteChannel chan = Files.newByteChannel(
            Paths.get(dfl(p)), StandardOpenOption.READ)) {
        chan.position(6);
        int cc = 0;
        while (true) {
            try {
                if (AuthTime.readFrom(chan) != null) cc++;
            } catch (BufferUnderflowException e) {
                break;
            }
        }
        return cc;
    } catch (IOException ioe) {
        return 0;
    }
}
 
Example #3
Source File: TextSource.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected void startReading(ReadableByteChannel channel) throws IOException {
  this.inChannel = channel;
  // If the first offset is greater than zero, we need to skip bytes until we see our
  // first delimiter.
  long startOffset = getCurrentSource().getStartOffset();
  if (startOffset > 0) {
    checkState(
        channel instanceof SeekableByteChannel,
        "%s only supports reading from a SeekableByteChannel when given a start offset"
            + " greater than 0.",
        TextSource.class.getSimpleName());
    long requiredPosition = startOffset - 1;
    if (delimiter != null && startOffset >= delimiter.length) {
      // we need to move back the offset of at worse delimiter.size to be sure to see
      // all the bytes of the delimiter in the call to findDelimiterBounds() below
      requiredPosition = startOffset - delimiter.length;
    }
    ((SeekableByteChannel) channel).position(requiredPosition);
    findDelimiterBounds();
    buffer = buffer.substring(endOfDelimiterInBuffer);
    startOfNextRecord = requiredPosition + endOfDelimiterInBuffer;
    endOfDelimiterInBuffer = 0;
    startOfDelimiterInBuffer = 0;
  }
}
 
Example #4
Source File: DflCache.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void expunge(Path p, KerberosTime currTime)
        throws IOException {
    Path p2 = Files.createTempFile(p.getParent(), "rcache", null);
    try (SeekableByteChannel oldChan = Files.newByteChannel(p);
            SeekableByteChannel newChan = createNoClose(p2)) {
        long timeLimit = currTime.getSeconds() - readHeader(oldChan);
        while (true) {
            try {
                AuthTime at = AuthTime.readFrom(oldChan);
                if (at.ctime > timeLimit) {
                    ByteBuffer bb = ByteBuffer.wrap(at.encode(true));
                    newChan.write(bb);
                }
            } catch (BufferUnderflowException e) {
                break;
            }
        }
    }
    makeMine(p2);
    Files.move(p2, p,
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.ATOMIC_MOVE);
}
 
Example #5
Source File: DflCache.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void expunge(Path p, KerberosTime currTime)
        throws IOException {
    Path p2 = Files.createTempFile(p.getParent(), "rcache", null);
    try (SeekableByteChannel oldChan = Files.newByteChannel(p);
            SeekableByteChannel newChan = createNoClose(p2)) {
        long timeLimit = currTime.getSeconds() - readHeader(oldChan);
        while (true) {
            try {
                AuthTime at = AuthTime.readFrom(oldChan);
                if (at.ctime > timeLimit) {
                    ByteBuffer bb = ByteBuffer.wrap(at.encode(true));
                    newChan.write(bb);
                }
            } catch (BufferUnderflowException e) {
                break;
            }
        }
    }
    makeMine(p2);
    Files.move(p2, p,
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.ATOMIC_MOVE);
}
 
Example #6
Source File: AuthTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads an LC style string from a channel, which is a int32 length
 * plus a UTF-8 encoded string possibly ends with \0.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
private static String readStringWithLength(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.nativeOrder());
    chan.read(bb);
    bb.flip();
    int len = bb.getInt();
    if (len > 1024) {
        // Memory attack? The string should be fairly short.
        throw new IOException("Invalid string length");
    }
    bb = ByteBuffer.allocate(len);
    if (chan.read(bb) != len) {
        throw new IOException("Not enough string");
    }
    byte[] data = bb.array();
    return (data[len-1] == 0)?
            new String(data, 0, len-1, StandardCharsets.UTF_8):
            new String(data, StandardCharsets.UTF_8);
}
 
Example #7
Source File: FFmpegTest.java    From Jaffree with Apache License 2.0 6 votes vote down vote up
@Test
public void testChannelOutput() throws IOException {
    Path tempDir = Files.createTempDirectory("jaffree");
    Path outputPath = tempDir.resolve("channel.mp4");

    LOGGER.debug("Will write to " + outputPath);

    try (SeekableByteChannel channel = Files.newByteChannel(outputPath, CREATE, WRITE, READ, TRUNCATE_EXISTING)) {
        FFmpegResult result = FFmpeg.atPath(BIN)
                .addInput(
                        UrlInput.fromPath(VIDEO_MP4)
                )
                .addOutput(
                        new ChannelOutput("channel.mp4", channel)
                )
                .setOverwriteOutput(true)
                .setLogLevel(LogLevel.INFO)
                .execute();

        Assert.assertNotNull(result);
        Assert.assertNotNull(result.getVideoSize());
    }

    Assert.assertTrue(Files.exists(outputPath));
    Assert.assertTrue(Files.size(outputPath) > 1000);
}
 
Example #8
Source File: IsmReaderImpl.java    From beam with Apache License 2.0 6 votes vote down vote up
private ShardAwareIsmPrefixReaderIterator(
    List<?> keyComponents, SeekableByteChannel rawChannel, SideInputReadCounter readCounter)
    throws IOException {
  super(keyComponents, readCounter);
  checkState(
      shardOffsetToShardMap.size() > 0,
      "Expected that shard offset to shard map has been initialized and is not empty.");

  this.rawChannel = rawChannel;
  this.shardEntries = shardOffsetToShardMap.values().iterator();
  IsmShard firstShard = shardEntries.next();
  delegate =
      new WithinShardIsmReaderIterator(
          rawChannel,
          new RandomAccessData(),
          firstShard.getBlockOffset(),
          firstShard.getIndexOffset(),
          readCounter);
}
 
Example #9
Source File: FileBasedSourceTest.java    From beam with Apache License 2.0 6 votes vote down vote up
public LineReader(ReadableByteChannel channel) throws IOException {
  buf = ByteBuffer.allocate(BUF_SIZE);
  buf.flip();

  boolean removeLine = false;
  // If we are not at the beginning of a line, we should ignore the current line.
  if (channel instanceof SeekableByteChannel) {
    SeekableByteChannel seekChannel = (SeekableByteChannel) channel;
    if (seekChannel.position() > 0) {
      // Start from one character back and read till we find a new line.
      seekChannel.position(seekChannel.position() - 1);
      removeLine = true;
    }
    nextLineStart = seekChannel.position();
  }
  this.channel = channel;
  if (removeLine) {
    nextLineStart += readNextLine(new ByteArrayOutputStream());
  }
}
 
Example #10
Source File: AuthTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads an LC style string from a channel, which is a int32 length
 * plus a UTF-8 encoded string possibly ends with \0.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
private static String readStringWithLength(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.nativeOrder());
    chan.read(bb);
    bb.flip();
    int len = bb.getInt();
    if (len > 1024) {
        // Memory attack? The string should be fairly short.
        throw new IOException("Invalid string length");
    }
    bb = ByteBuffer.allocate(len);
    if (chan.read(bb) != len) {
        throw new IOException("Not enough string");
    }
    byte[] data = bb.array();
    return (data[len-1] == 0)?
            new String(data, 0, len-1, StandardCharsets.UTF_8):
            new String(data, StandardCharsets.UTF_8);
}
 
Example #11
Source File: AuthTime.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads an LC style string from a channel, which is a int32 length
 * plus a UTF-8 encoded string possibly ends with \0.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
private static String readStringWithLength(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.nativeOrder());
    chan.read(bb);
    bb.flip();
    int len = bb.getInt();
    if (len > 1024) {
        // Memory attack? The string should be fairly short.
        throw new IOException("Invalid string length");
    }
    bb = ByteBuffer.allocate(len);
    if (chan.read(bb) != len) {
        throw new IOException("Not enough string");
    }
    byte[] data = bb.array();
    return (data[len-1] == 0)?
            new String(data, 0, len-1, StandardCharsets.UTF_8):
            new String(data, StandardCharsets.UTF_8);
}
 
Example #12
Source File: AuthTime.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads an LC style string from a channel, which is a int32 length
 * plus a UTF-8 encoded string possibly ends with \0.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
private static String readStringWithLength(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.nativeOrder());
    chan.read(bb);
    bb.flip();
    int len = bb.getInt();
    if (len > 1024) {
        // Memory attack? The string should be fairly short.
        throw new IOException("Invalid string length");
    }
    bb = ByteBuffer.allocate(len);
    if (chan.read(bb) != len) {
        throw new IOException("Not enough string");
    }
    byte[] data = bb.array();
    return (data[len-1] == 0)?
            new String(data, 0, len-1, StandardCharsets.UTF_8):
            new String(data, StandardCharsets.UTF_8);
}
 
Example #13
Source File: FFmpegTest.java    From Jaffree with Apache License 2.0 6 votes vote down vote up
@Test
public void testChannelInput() throws IOException {
    Path tempDir = Files.createTempDirectory("jaffree");
    Path outputPath = tempDir.resolve("channel.mp4");

    try (SeekableByteChannel channel = Files.newByteChannel(VIDEO_MP4, READ)) {
        FFmpegResult result = FFmpeg.atPath(BIN)
                .addInput(
                        new ChannelInput("testChannelInput.mp4", channel)
                )
                .addOutput(
                        UrlOutput.toPath(outputPath)
                )
                .setLogLevel(LogLevel.DEBUG)
                .execute();

        Assert.assertNotNull(result);
        Assert.assertNotNull(result.getVideoSize());
    }

    Assert.assertTrue(Files.exists(outputPath));
    Assert.assertTrue(Files.size(outputPath) > 1000);
}
 
Example #14
Source File: FileUtils.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Read bunch of last records from file.
 *
 * @param userDataFile - file to read
 * @param count        - number of records to read
 * @param skip         - number of entries to skip from the end
 * @return - byte buffer with data
 */
public static ByteBuffer read(Path userDataFile, int count, int skip) throws IOException {
    int size = (int) Files.size(userDataFile);
    int expectedMinimumLength = (count + skip) * SIZE_OF_REPORT_ENTRY;
    int diff = size - expectedMinimumLength;
    int startReadIndex = Math.max(0, diff);
    int bufferSize = diff < 0 ? count * SIZE_OF_REPORT_ENTRY + diff : count * SIZE_OF_REPORT_ENTRY;
    if (bufferSize <= 0) {
        return null;
    }

    ByteBuffer buf = ByteBuffer.allocate(bufferSize);

    try (SeekableByteChannel channel = Files.newByteChannel(userDataFile, EnumSet.of(READ))) {
        channel.position(startReadIndex)
                .read(buf);
        ((Buffer) buf).flip();
        return buf;
    }
}
 
Example #15
Source File: ReplayCachePrecise.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0,
                "1111111111111111");
        AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
                "2222222222222222");
        KerberosTime now = new KerberosTime(time(0)*1000L);

        // When all new styles, must exact match
        ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
        cache.checkAndStore(now, a1);
        cache.checkAndStore(now, a2);

        // When only old style in cache, partial match
        cache = ReplayCache.getInstance("dfl:./c2");
        cache.checkAndStore(now, a1);
        // A small surgery to remove the new style from the cache file
        SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"),
                StandardOpenOption.WRITE,
                StandardOpenOption.READ);
        ch.position(6);
        ch.write(ByteBuffer.wrap(a1.encode(false)));
        ch.truncate(ch.position());
        ch.close();
        try {
            cache.checkAndStore(now, a2);
            throw new Exception();
        } catch (KrbException ke) {
            // Correct
            System.out.println(ke);
        }
    }
 
Example #16
Source File: DflCache.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("try")
private static void create(Path p) throws IOException {
    try (SeekableByteChannel newChan = createNoClose(p)) {
        // Do nothing, wait for close
    }
    makeMine(p);
}
 
Example #17
Source File: DflCache.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example #18
Source File: JournaledCoalescer.java    From emissary with Apache License 2.0 5 votes vote down vote up
/**
 * Copies all bytes from all paths that match to an output stream.
 *
 * @param journal The journal to combine in the output stream
 * @param rolledOutput The OutputStream object to use
 */
protected void combineFiles(Journal journal, SeekableByteChannel rolledOutput) throws IOException {
    long startPos = rolledOutput.position();
    JournalEntry last = journal.getLastEntry();
    if (last == null) {
        LOG.debug("Empty Journal encountered. {}", journal);
        return;
    }
    long offset = last.getOffset();
    Path p = Paths.get(last.getVal());
    LOG.debug("Reading from path {}", p);
    try (FileChannel part = FileChannel.open(p, READ)) {
        long partSize = Files.size(p);
        if (partSize < last.getOffset()) {
            JournalEntry lastGood = journal.getLastValidEntry(partSize);
            offset = lastGood.getOffset();
            LOG.warn("The bgpart file, {}, likely lost data due to a crash. Part size: {}, Expected {}, Actual: {}", last.getVal(), partSize,
                    last.getOffset(), offset);
        }
        long xfer;
        // for loop due to contract of channel.transferTo()
        for (long count = offset; count > 0L;) {
            xfer = part.transferTo(part.position(), count, rolledOutput);
            part.position(part.position() + xfer);
            count -= xfer;
            if (part.position() == partSize && count > 0L) {
                throw new IOException("Premature EOF. Expected " + offset + ", but only transferred " + partSize);
            }
        }
        LOG.debug("Successfully appended {} bytes from {} to output file.", offset, p);
    } catch (IOException ex) {
        LOG.error("Exception attempting to transfer {} bytes from {} to output", offset, p.toString(), ex);
        renameToError(p);
        renameToError(journal.getJournalPath());
        rolledOutput.truncate(startPos);
        rolledOutput.position(startPos);
    }
}
 
Example #19
Source File: Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = MemoryUtil.memAlloc((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (
            InputStream source = Utils.class.getResourceAsStream(resource);
            ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = MemoryUtil.memAlloc(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example #20
Source File: CipherFileChannel.java    From encfs4j with Apache License 2.0 5 votes vote down vote up
@Override
public SeekableByteChannel truncate(long length) throws IOException {
	// as long as the transformed channel is not initialized truncate the
	// persistent channel
	SeekableByteChannel ch = this.trafoChannel == null ? this.persistentChannel
			.truncate(length) : this.trafoChannel.truncate(length);
	return this;
}
 
Example #21
Source File: DflCache.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static int readHeader(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(6);
    chan.read(bb);
    if (bb.getShort(0) != KRB5_RV_VNO) {
        throw new IOException("Not correct rcache version");
    }
    bb.order(ByteOrder.nativeOrder());
    return bb.getInt(2);
}
 
Example #22
Source File: CustomOptions.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example #23
Source File: UnixSecureDirectoryStream.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Opens file in this directory
 */
@Override
public SeekableByteChannel newByteChannel(Path obj,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    UnixPath file = getName(obj);

    int mode = UnixFileModeAttribute
        .toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs);

    // path for permission check
    String pathToCheck = ds.directory().resolve(file).getPathForPermissionCheck();

    ds.readLock().lock();
    try {
        if (!ds.isOpen())
            throw new ClosedDirectoryStreamException();
        try {
            return UnixChannelFactory.newFileChannel(dfd, file, pathToCheck, options, mode);
        } catch (UnixException x) {
            x.rethrowAsIOException(file);
            return null; // keep compiler happy
        }
    } finally {
        ds.readLock().unlock();
    }
}
 
Example #24
Source File: FilePlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@TruffleBoundary(transferToInterpreterOnException = false)
private static long getSize(final SeekableByteChannel channel) {
    try {
        return channel.size();
    } catch (final IOException e) {
        log("Failed to get file size", e);
        throw PrimitiveFailed.GENERIC_ERROR;
    }
}
 
Example #25
Source File: FileIO.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link SeekableByteChannel} equivalent to {@link #open}, but fails if this file is
 * not {@link MatchResult.Metadata#isReadSeekEfficient seekable}.
 */
public SeekableByteChannel openSeekable() throws IOException {
  checkState(
      getMetadata().isReadSeekEfficient(),
      "The file %s is not seekable",
      metadata.resourceId());
  return (SeekableByteChannel) open();
}
 
Example #26
Source File: UnixSecureDirectoryStream.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Opens file in this directory
 */
@Override
public SeekableByteChannel newByteChannel(Path obj,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    UnixPath file = getName(obj);

    int mode = UnixFileModeAttribute
        .toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs);

    // path for permission check
    String pathToCheck = ds.directory().resolve(file).getPathForPermissionCheck();

    ds.readLock().lock();
    try {
        if (!ds.isOpen())
            throw new ClosedDirectoryStreamException();
        try {
            return UnixChannelFactory.newFileChannel(dfd, file, pathToCheck, options, mode);
        } catch (UnixException x) {
            x.rethrowAsIOException(file);
            return null; // keep compiler happy
        }
    } finally {
        ds.readLock().unlock();
    }
}
 
Example #27
Source File: ThrottledGoogleCloudStorage.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public SeekableByteChannel open(
    StorageResourceId resourceId, GoogleCloudStorageReadOptions readOptions)
    throws IOException {
  throttle(StorageOperation.OPEN_OBJECT);
  return wrappedGcs.open(resourceId, readOptions);
}
 
Example #28
Source File: AuthTime.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads an AuthTime or AuthTimeWithHash object from a channel.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
public static AuthTime readFrom(SeekableByteChannel chan)
        throws IOException {
    String client = readStringWithLength(chan);
    String server = readStringWithLength(chan);
    ByteBuffer bb = ByteBuffer.allocate(8);
    chan.read(bb);
    bb.order(ByteOrder.nativeOrder());
    int cusec = bb.getInt(0);
    int ctime = bb.getInt(4);
    if (client.isEmpty()) {
        StringTokenizer st = new StringTokenizer(server, " :");
        if (st.countTokens() != 6) {
            throw new IOException("Incorrect rcache style");
        }
        st.nextToken();
        String hash = st.nextToken();
        st.nextToken();
        client = st.nextToken();
        st.nextToken();
        server = st.nextToken();
        return new AuthTimeWithHash(
                client, server, ctime, cusec, hash);
    } else {
        return new AuthTime(
                client, server, ctime, cusec);
    }
}
 
Example #29
Source File: PassThroughFileSystem.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example #30
Source File: GoogleCloudStorageFileSystemIntegrationTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void read_failure_ifObjectWasModifiedDuringRead() throws IOException {
  URI testObject = gcsiHelper.getUniqueObjectUri("generation-strict");
  String message1 = "Hello world!\n";
  String message2 = "Sayonara world!\n";

  gcsiHelper.writeTextFile(testObject, message1);
  int offset = 5;
  // These read options force the readChannel to open stream again on second read.
  GoogleCloudStorageReadOptions readOptions =
      GoogleCloudStorageReadOptions.builder()
          .setFadvise(Fadvise.RANDOM)
          .setMinRangeRequestSize(0)
          .build();
  try (SeekableByteChannel readChannel = gcsiHelper.open(testObject, readOptions)) {
    String read1 = gcsiHelper.readText(readChannel, 0, offset, false);
    assertWithMessage("partial read mismatch")
        .that(read1)
        .isEqualTo(message1.substring(0, offset));
    gcsiHelper.writeTextFileOverwriting(testObject, message2);
    FileNotFoundException expected =
        assertThrows(
            FileNotFoundException.class,
            () -> gcsiHelper.readText(readChannel, offset, message1.length() - offset, true));
    assertThat(expected)
        .hasMessageThat()
        .contains(
            "Note, it is possible that the live version is still available"
                + " but the requested generation is deleted.");
  }
}