Java Code Examples for java.nio.channels.SeekableByteChannel#position()
The following examples show how to use
java.nio.channels.SeekableByteChannel#position() .
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: FileBasedSource.java From beam with Apache License 2.0 | 6 votes |
@Override protected final boolean startImpl() throws IOException { FileBasedSource<T> source = getCurrentSource(); this.channel = FileSystems.open(source.getSingleFileMetadata().resourceId()); if (channel instanceof SeekableByteChannel) { SeekableByteChannel seekChannel = (SeekableByteChannel) channel; seekChannel.position(source.getStartOffset()); } else { // Channel is not seekable. Must not be a subrange. checkArgument( source.mode != Mode.SINGLE_FILE_OR_SUBRANGE, "Subrange-based sources must only be defined for file types that support seekable " + " read channels"); checkArgument( source.getStartOffset() == 0, "Start offset %s is not zero but channel for reading the file is not seekable.", source.getStartOffset()); } startReading(channel); // Advance once to load the first record. return advanceImpl(); }
Example 2
Source File: TestDocUseCases.java From jsr203-hadoop with Apache License 2.0 | 6 votes |
@Test public void testWriteBufferedAndRead() throws IOException { Path rootPath = Paths.get(clusterUri); Path file = rootPath.resolve(rootPath.resolve("temp_file.txt")); Charset charset = Charset.forName("US-ASCII"); String s = "this is a test"; BufferedWriter writer = Files.newBufferedWriter(file, charset); writer.write(s, 0, s.length()); writer.close(); List<String> lines = Files.readAllLines(file, charset); assertEquals(1, lines.size()); assertEquals(s, lines.get(0)); // test positioned reads int offset = 8; byte[] contents = new byte[s.length() - offset]; ByteBuffer buffer = ByteBuffer.wrap(contents); SeekableByteChannel seekableByteChannel = Files.newByteChannel(file); seekableByteChannel.position(offset); int read = seekableByteChannel.read(buffer); assertEquals(s.length() - offset, read); assertEquals("a test", new String(contents, charset)); }
Example 3
Source File: RedissonBinaryStreamTest.java From redisson with Apache License 2.0 | 6 votes |
@Test public void testChannelTruncate() throws IOException { RBinaryStream stream = redisson.getBinaryStream("test"); SeekableByteChannel c = stream.getChannel(); c.write(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7})); assertThat(c.size()).isEqualTo(7); c.truncate(3); c.position(0); c.truncate(10); ByteBuffer b = ByteBuffer.allocate(3); c.read(b); byte[] bb = new byte[3]; b.flip(); b.get(bb); assertThat(c.size()).isEqualTo(3); assertThat(bb).isEqualTo(new byte[]{1, 2, 3}); }
Example 4
Source File: GoogleCloudStorageIntegrationHelper.java From hadoop-connectors with Apache License 2.0 | 6 votes |
/** Helper that reads text from a given SeekableByteChannel. */ protected String readText( SeekableByteChannel readChannel, int offset, int len, boolean checkOverflow) throws IOException { int bufferSize = len + (checkOverflow ? 1 : 0); ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize); if (offset > 0) { readChannel.position(offset); } int numBytesRead = readChannel.read(readBuffer); assertWithMessage("readText: read size mismatch").that(numBytesRead).isEqualTo(len); readBuffer.flip(); return StandardCharsets.UTF_8.decode(readBuffer).toString(); }
Example 5
Source File: ReplayCachePrecise.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0, "HASH", "1111111111111111"); AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0, "HASH", "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 6
Source File: MXFFiles.java From regxmllib with BSD 2-Clause "Simplified" License | 5 votes |
/** * Seeks to the first byte of the Header partition, assuming the current position of the * channel is within the run-in (SMPTE ST 377-1 Section 6.5) * * @param mxffile Channel containing an MXF file * @return Offset of the first byte of the Header Partition, or -1 if * the Header Partition was not found * @throws IOException */ public static long seekHeaderPartition(SeekableByteChannel mxffile) throws IOException { ByteBuffer ulbytes = ByteBuffer.allocate(16); long offset = mxffile.position(); while (mxffile.read(ulbytes) == ulbytes.limit() && offset <= 65536) { UL ul = new UL(ulbytes.array()); if (ul.equalsWithMask(PartitionPack.getKey(), 65248 /* first eleven bytes minus the version byte */ )) { mxffile.position(offset); return offset; } mxffile.position(++offset); } return -1; }
Example 7
Source File: ReplayCachePrecise.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
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 8
Source File: ContentUtils.java From mycore with GNU General Public License v3.0 | 5 votes |
/** * Consumes the content and writes it to the ServletOutputStream. * * @param content The source resource * @param out The outputBufferSize stream to write to * @param range Range the client wanted to retrieve */ static void copy(final MCRContent content, final OutputStream out, final Range range, // TODO: beautify this final int inputBufferSize, final int outputBufferSize) throws IOException { if (content.isReusable()) { try (ReadableByteChannel readableByteChannel = content.getReadableByteChannel()) { if (readableByteChannel instanceof SeekableByteChannel) { endCurrentTransaction(); SeekableByteChannel seekableByteChannel = (SeekableByteChannel) readableByteChannel; seekableByteChannel.position(range.start); long bytesToCopy = range.end - range.start + 1; while (bytesToCopy > 0) { ByteBuffer byteBuffer; if (bytesToCopy > DEFAULT_BUFFER_SIZE) { byteBuffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE); } else { byteBuffer = ByteBuffer.allocate((int) bytesToCopy); } int bytesRead = seekableByteChannel.read(byteBuffer); bytesToCopy -= bytesRead; out.write(byteBuffer.array()); } return; } } } try (InputStream resourceInputStream = content.getInputStream(); InputStream in = isInputStreamBuffered(resourceInputStream, content) ? resourceInputStream : new BufferedInputStream(resourceInputStream, inputBufferSize)) { endCurrentTransaction(); final IOException exception = copyRange(in, out, 0, range.start, range.end, outputBufferSize); if (exception != null) { throw exception; } } }
Example 9
Source File: ReplayCachePrecise.java From hottub with GNU General Public License v2.0 | 5 votes |
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 10
Source File: ReplayCachePrecise.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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 11
Source File: ReplayCachePrecise.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
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 12
Source File: ReplayCachePrecise.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 13
Source File: RedissonBinaryStreamTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testChannelPosition() throws IOException { RBinaryStream stream = redisson.getBinaryStream("test"); SeekableByteChannel c = stream.getChannel(); c.write(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7})); c.position(3); ByteBuffer b = ByteBuffer.allocate(3); c.read(b); assertThat(c.position()).isEqualTo(6); byte[] bb = new byte[3]; b.flip(); b.get(bb); assertThat(bb).isEqualTo(new byte[]{4, 5, 6}); }
Example 14
Source File: JournaledCoalescer.java From emissary with Apache License 2.0 | 5 votes |
/** * 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 15
Source File: ParserFactory.java From emissary with Apache License 2.0 | 5 votes |
/** * Return the key identification type fo the data in the channel * * @param channel the channel containing bytes to identify * @return string matching the keys in ParserFactory.cfg */ public String identify(SeekableByteChannel channel) { if (idEngine != null) { try { long pos = channel.position(); byte[] buf = Executrix.readDataFromChannel(channel, pos, idEngine.DATA_ID_STR_SZ); channel.position(pos); return idEngine.identify(buf); } catch (IOException e) { logger.warn("Unable to reposition file channel", e); } } return DataIdentifier.UNKNOWN_TYPE; }
Example 16
Source File: IsmReaderImpl.java From beam with Apache License 2.0 | 5 votes |
/** * Seeks into the channel intelligently by either resetting the position or reading and discarding * bytes. */ private static void position(SeekableByteChannel inChannel, long newPosition) throws IOException { long currentPosition = inChannel.position(); // If just doing a read is cheaper discarding the bytes lets just do the read if (currentPosition < newPosition && newPosition - currentPosition <= SEEK_VS_READ) { ByteStreams.skipFully(Channels.newInputStream(inChannel), newPosition - currentPosition); } else { // Otherwise we will perform a seek inChannel.position(newPosition); } }
Example 17
Source File: Bug1399.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@ExpectWarning("OS_OPEN_STREAM") void test3(File f, byte b) throws IOException { SeekableByteChannel c = Files.newByteChannel(Paths.get("")); c.position(); }
Example 18
Source File: LimitedNioChannel.java From gadtry with Apache License 2.0 | 4 votes |
public LimitedNioChannel(SeekableByteChannel readChannel, long position, long limit) throws IOException { this(readChannel, limit); readChannel.position(position); }
Example 19
Source File: GoogleCloudStorageTest.java From hadoop-connectors with Apache License 2.0 | 4 votes |
/** Test in-place forward seeks larger than seek buffer but smaller than limit. */ @Test public void testInplaceSeekLargerThanSeekBuffer() throws Exception { byte[] testData = new byte[2 * GoogleCloudStorageReadChannel.SKIP_BUFFER_SIZE]; new Random().nextBytes(testData); MockHttpTransport transport = mockTransport(dataResponse(testData)); GoogleCloudStorage gcs = mockedGcs(transport); GoogleCloudStorageReadOptions readOptions = GoogleCloudStorageReadOptions.builder() .setFastFailOnNotFound(false) .setInplaceSeekLimit(testData.length) .build(); SeekableByteChannel readChannel = gcs.open(new StorageResourceId(BUCKET_NAME, OBJECT_NAME), readOptions); byte[] actualData = new byte[1]; int bytesRead = readChannel.read(ByteBuffer.wrap(actualData)); assertThat(bytesRead).isEqualTo(1); assertThat(actualData).isEqualTo(new byte[] {testData[0]}); // Jump SKIP_BUFFER_SIZE + 3 bytes forwards; this should be done in-place without any // new executeMedia() call. int jumpPosition = GoogleCloudStorageReadChannel.SKIP_BUFFER_SIZE + 3; readChannel.position(jumpPosition); assertThat(readChannel.position()).isEqualTo(jumpPosition); actualData = new byte[2]; bytesRead = readChannel.read(ByteBuffer.wrap(actualData)); assertThat(bytesRead).isEqualTo(2); assertThat(actualData) .isEqualTo(new byte[] {testData[jumpPosition], testData[jumpPosition + 1]}); assertThat(trackingHttpRequestInitializer.getAllRequestStrings()) .containsExactly(getMediaRequestString(BUCKET_NAME, OBJECT_NAME)) .inOrder(); }
Example 20
Source File: Bug1399.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@ExpectWarning("OS_OPEN_STREAM") void test4(File f, byte b) throws IOException { SeekableByteChannel c = Files.newByteChannel(Paths.get(""), StandardOpenOption.APPEND); c.position(); }