Java Code Examples for java.nio.channels.SeekableByteChannel#read()
The following examples show how to use
java.nio.channels.SeekableByteChannel#read() .
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: AuthTime.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
/** * 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 2
Source File: AuthTime.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * 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 3
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 4
Source File: AuthTime.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * 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 5
Source File: GoogleCloudStorageTest.java From hadoop-connectors with Apache License 2.0 | 6 votes |
@Test public void testChannelClosedException() throws IOException { final int totalBytes = 1200; String bucketName = getSharedBucketName(); StorageResourceId resourceId = new StorageResourceId(bucketName, "testChannelClosedException_Object"); writeObject(rawStorage, resourceId, /* objectSize= */ totalBytes); byte[] readArray = new byte[totalBytes]; SeekableByteChannel readableByteChannel = rawStorage.open(resourceId); ByteBuffer readBuffer = ByteBuffer.wrap(readArray); readBuffer.limit(5); readableByteChannel.read(readBuffer); assertThat(readableByteChannel.position()).isEqualTo(readBuffer.position()); readableByteChannel.close(); readBuffer.clear(); assertThrows(ClosedChannelException.class, () -> readableByteChannel.read(readBuffer)); }
Example 6
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 7
Source File: DflCache.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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 8
Source File: AuthTime.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * 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 9
Source File: AuthTime.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * 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 10
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 11
Source File: GcsPinotFS.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Override public void copyFromLocalFile(File srcFile, URI dstUri) throws Exception { LOGGER.info("Copying file {} to uri {}", srcFile.getAbsolutePath(), dstUri); Bucket bucket = getBucket(dstUri); Blob blob = bucket.create(sanitizePath(dstUri.getPath()), new byte[0]); WriteChannel writeChannel = blob.writer(); writeChannel.setChunkSize(BUFFER_SIZE); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); SeekableByteChannel channel = Files.newByteChannel(srcFile.toPath()); for (int bytesRead = channel.read(buffer); bytesRead != -1; bytesRead = channel.read(buffer)) { buffer.flip(); writeChannel.write(buffer); buffer.clear(); } writeChannel.close(); }
Example 12
Source File: PicardIndexedFastaSequenceFile.java From chipster with MIT License | 5 votes |
/** * Reads a sequence of bytes from this channel into the given buffer, * starting at the given file position. * @param channel the channel to read from * @param buffer the buffer into which bytes are to be transferred * @param position the position to start reading at * @return the number of bytes read * @throws IOException if an I/O error occurs while reading */ private static int readFromPosition(final SeekableByteChannel channel, final ByteBuffer buffer, long position) throws IOException { if (channel instanceof FileChannel) { // special case to take advantage of native code path return ((FileChannel) channel).read(buffer,position); } else { long oldPos = channel.position(); try { channel.position(position); return channel.read(buffer); } finally { channel.position(oldPos); } } }
Example 13
Source File: AuthTime.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * 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 14
Source File: DflCache.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
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 15
Source File: AuthTime.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * 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 16
Source File: BufferUtils.java From THULAC-Java with MIT License | 5 votes |
/** * Read ints from {@code channel} using {@code buf} as buffer and putting them * sequentially into the array of {@code int[]} represented by {@code arrays}.<br> * {@code buf} is always in read mode after this method returns, that is, users * have to call {@code buf.flip()} first if they wish to reuse it. {@code * channel} is NOT closed after this method returns (since the EOF might not have been * reached yet), therefore users should close it manually.<br> * * @param channel * The {@link FileChannel} to read from. * @param buf * The {@link ByteBuffer} to use as buffer. * @param arrays * The array of {@code int[]} to store the read ints. * * @return A return value of {@code true} means that all the arrays are successfully * filled with data read from {@code channel}, while {@code false} means that the * EOF is reached before all the arrays are filled. In special case that all arrays * are filled and EOF is reached, {@code true} is returned. * * @throws IOException * If an exception is thrown while reading from {@code channel}. * @throws NullPointerException * If either channel is null, buf is null, or any element of {@code arrays} is * null. */ public static boolean readInts( SeekableByteChannel channel, ByteBuffer buf, int[]... arrays) throws IOException { int position = 0, offset = 0; int[] current = arrays[position]; int currentLeft = current.length, readBytes, readInts; while (true) { // read buffer readBytes = channel.read(buf); // if EOF is reached and there are still arrays left not filled if (readBytes == -1) return false; buf.flip(); IntBuffer intBuf = buf.asIntBuffer(); readInts = readBytes >> 2; // copy buffer content to arrays while (readInts > 0) { int getLen = Math.min(readInts, currentLeft); intBuf.get(current, offset, getLen); offset += getLen; readInts -= getLen; currentLeft -= getLen; if (currentLeft == 0) { // if current array is filled ++position; if (position == arrays.length) { // if all arrays have been filled buf.clear(); return true; } current = arrays[position]; offset = 0; currentLeft = current.length; } } buf.clear(); } }
Example 17
Source File: DflCache.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
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 18
Source File: DflCache.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
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 19
Source File: MXFFiles.java From regxmllib with BSD 2-Clause "Simplified" License | 4 votes |
/** * Seeks to the footer partition, assuming the current position of the * channel is within the run-in (SMPTE ST 377-1 Section 6.5), the footer partition * offset is listed in the Header Partition Pack or a Random Index Pack is * present. * * @param mxffile Channel containing an MXF file * @return Offset of the Footer Partition, or -1 if a Footer Partition was not found * @throws IOException * @throws com.sandflow.smpte.klv.exceptions.KLVException */ public static long seekFooterPartition(SeekableByteChannel mxffile) throws IOException, KLVException { long headeroffset = seekHeaderPartition(mxffile); KLVInputStream kis = new KLVInputStream(Channels.newInputStream(mxffile)); Triplet t = kis.readTriplet(); if (t == null) { return -1; } PartitionPack pp = PartitionPack.fromTriplet(t); if (pp == null) { return -1; } if (pp.getFooterPartition() != 0) { mxffile.position(headeroffset + pp.getFooterPartition()); return mxffile.position(); } /* look for the RIP start */ mxffile.position(mxffile.size() - 4); ByteBuffer bytes = ByteBuffer.allocate(4); if (mxffile.read(bytes) != bytes.limit()) { return -1; } /* move to start of RIP */ mxffile.position(mxffile.size() - bytes.getInt(0)); /* read RIP */ kis = new KLVInputStream(Channels.newInputStream(mxffile)); t = kis.readTriplet(); if (t == null) { return -1; } RandomIndexPack rip = RandomIndexPack.fromTriplet(t); if (rip == null) { return -1; } mxffile.position(rip.getOffsets().get(rip.getOffsets().size() - 1).getOffset()); return mxffile.position(); }
Example 20
Source File: FilesNewByteChannelTest.java From ParallelGit with Apache License 2.0 | 4 votes |
@Nonnull private static byte[] readChannel(SeekableByteChannel channel) throws IOException { ByteBuffer buffer = ByteBuffer.allocate((int) channel.size()); channel.read(buffer); return buffer.array(); }