List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:kilim.http.HttpRequest.java
public void dumpBuffer(ByteBuffer buffer) { byte[] ba = buffer.array(); int len = buffer.position(); StringBuilder print = new StringBuilder(); for (int i = 0; i < len; i++) { print.append((char) ba[i]); }//from ww w .ja v a2 s. c o m log.debug(print); }
From source file:co.cask.cdap.client.rest.RestStreamWriter.java
@Override public ListenableFuture<Void> write(ByteBuffer buffer, Map<String, String> headers) throws IllegalArgumentException { Preconditions.checkArgument(buffer != null, "ByteBuffer parameter is null."); HttpEntity content;// ww w . j av a2 s. c o m if (buffer.hasArray()) { content = new ByteArrayEntity(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); content = new ByteArrayEntity(bytes); } return write(content, headers); }
From source file:com.yobidrive.diskmap.needles.Needle.java
public void putNeedleInBuffer(ByteBuffer result) throws Exception { int startPosition = result.position(); result.limit(result.capacity());//from w ww.j av a 2 s . co m result.putInt(MAGICSTART); result.putLong(needleNumber); result.put(flags); result.putInt(keyBytes.length); result.put(keyBytes); result.putInt(version == null ? 0 : version.toBytes().length); if (version != null) result.put(version.toBytes()); result.putInt(previousNeedle == null ? -1 : previousNeedle.getNeedleFileNumber()); // Chaining result.putLong(previousNeedle == null ? -1L : previousNeedle.getNeedleOffset()); // Chaining result.putInt(originalFileNumber); // Original needle location (for cleaning) result.putInt(originalSize); // Original needle size (for cleaning) result.putInt(data == null ? 0 : data.length); if (data != null) result.put(data); result.putInt(MAGICEND); result.put(hashMD5()); while (((result.position() - startPosition) % 256) > 0) { result.put(PADDING); } result.flip(); }
From source file:com.facebook.hive.orc.ReaderImpl.java
public ReaderImpl(FileSystem fs, Path path, Configuration conf) throws IOException { try {/*from w ww.ja v a 2s. c o m*/ this.fileSystem = fs; this.path = path; this.conf = conf; FSDataInputStream file = fs.open(path); long size = fs.getFileStatus(path).getLen(); int readSize = (int) Math.min(size, DIRECTORY_SIZE_GUESS); ByteBuffer buffer = ByteBuffer.allocate(readSize); InStream.read(file, size - readSize, buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); int psLen = buffer.get(readSize - 1); int psOffset = readSize - 1 - psLen; CodedInputStream in = CodedInputStream.newInstance(buffer.array(), buffer.arrayOffset() + psOffset, psLen); OrcProto.PostScript ps = OrcProto.PostScript.parseFrom(in); int footerSize = (int) ps.getFooterLength(); bufferSize = (int) ps.getCompressionBlockSize(); switch (ps.getCompression()) { case NONE: compressionKind = CompressionKind.NONE; break; case ZLIB: compressionKind = CompressionKind.ZLIB; break; case SNAPPY: compressionKind = CompressionKind.SNAPPY; break; case LZO: compressionKind = CompressionKind.LZO; break; default: throw new IllegalArgumentException("Unknown compression"); } codec = WriterImpl.createCodec(compressionKind); InputStream instream = InStream.create("footer", file, size - 1 - psLen - footerSize, footerSize, codec, bufferSize); footer = OrcProto.Footer.parseFrom(instream); inspector = new OrcLazyRowObjectInspector(0, footer.getTypesList()); file.close(); } catch (IndexOutOfBoundsException e) { /** * When a non ORC file is read by ORC reader, we get IndexOutOfBoundsException exception while * creating a reader. Caught that exception and checked the file header to see if the input * file was ORC or not. If its not ORC, throw a NotAnORCFileException with the file * attempted to be reading (thus helping to figure out which table-partition was being read). */ checkIfORC(fs, path); throw new IOException("Failed to create record reader for file " + path, e); } catch (IOException e) { throw new IOException("Failed to create record reader for file " + path, e); } }
From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java
@Override public void write(DataOutput out) throws IOException { super.write(out); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 4 + 8); bbuffer.putInt(getWidth());/*from w w w . j av a 2s .c o m*/ bbuffer.putInt(getHeight()); gzos.write(bbuffer.array(), 0, bbuffer.position()); for (int x = 0; x < getWidth(); x++) { bbuffer.clear(); for (int y = 0; y < getHeight(); y++) { bbuffer.putFloat(frequencies[x][y]); } gzos.write(bbuffer.array(), 0, bbuffer.position()); } gzos.close(); byte[] serializedData = baos.toByteArray(); out.writeInt(serializedData.length); out.write(serializedData); }
From source file:com.mycustomloader.vsamloader.VSAMLoader.java
private void readField(ByteBuffer buf, int fieldID) { if (mRequiredColumns == null || (mRequiredColumns.length > fieldID && mRequiredColumns[fieldID])) { byte[] bytes = new byte[buf.position()]; buf.rewind();//from w w w.j a v a2s .c o m buf.get(bytes, 0, bytes.length); mProtoTuple.add(new DataByteArray(bytes)); } buf.clear(); }
From source file:com.gamesalutes.utils.ByteUtils.java
/** * Extends the size of <code>buf</code> to at least meet <code>minCap</code>. * If <code>buf</code> is too small, then a new buffer is allocated and * any existing contents in <code>buf</code> will be transfered. The position * of the new buffer will be that of the old buffer if it was not <code>null</code>, and * the previous mark will be discarded if one was set. * /* w w w . j a v a 2 s.com*/ * @param buf the input <code>ByteBuffer</code> * @param minCap the minimum capacity * @return a <code>ByteBuffer</code> that can meet <code>minCap</code> */ public static ByteBuffer growBuffer(ByteBuffer buf, int minCap) { int myLimit = buf != null ? buf.limit() : 0; // limit can accomidate capacity requirements if (buf != null && myLimit >= minCap) return buf; int myCap = buf != null ? buf.capacity() : 0; // capacity can accomidate but limit is too small if (buf != null && myCap >= minCap) { buf.limit(myCap); return buf; } else //if(myCap < minCap) { ByteBuffer newBuffer = null; if (myCap == 0) myCap = 1; while (myCap < minCap) myCap <<= 1; if (buf != null && buf.isDirect()) newBuffer = ByteBuffer.allocateDirect(myCap); else newBuffer = ByteBuffer.allocate(myCap); // copy contents of original buffer if (buf != null) { int pos = buf.position(); buf.clear(); newBuffer.put(buf); newBuffer.position(pos); } return newBuffer; } }
From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java
private void checkResponseStatus(HttpResponse response) { StatusLine statusLine = response.getStatusLine(); int status = statusLine.getStatusCode(); if (statusLine.getStatusCode() >= 400) { String error;/* ww w .j a va2s .com*/ try { ByteBuffer buf = IO.readWholeStream(new BufferedInputStream(response.getEntity().getContent()), 1024); if (buf.hasArray()) { error = new String(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining(), UTF_8); } else { final byte[] b = new byte[buf.remaining()]; buf.duplicate().get(b); error = new String(b, UTF_8); } } catch (IOException e) { error = statusLine.getReasonPhrase(); } throw new RuntimeException("Status: " + status + " " + error); } assertEquals(200, status); }
From source file:com.spectralogic.ds3client.helpers.channels.WindowedSeekableByteChannel_Test.java
@Test(timeout = 1000) public void readPositionTracking() throws IOException { try (final SeekableByteChannel channel = stringToChannel("aabbbcccc")) { final Object lock = new Object(); try (final WindowedSeekableByteChannel window = new WindowedSeekableByteChannel(channel, lock, 2L, 7L)) {//from www. j a v a 2 s . c om final byte[] bytes = new byte[10]; final ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.limit(3); assertThat(window.read(buffer), is(3)); assertThat(window.position(), is(3L)); assertThat(buffer.position(), is(3)); assertThat(buffer.limit(), is(3)); assertThat(new String(bytes, 0, 3, Charset.forName("UTF-8")), is("bbb")); buffer.limit(10); assertThat(window.read(buffer), is(4)); assertThat(window.position(), is(7L)); assertThat(buffer.position(), is(7)); assertThat(buffer.limit(), is(10)); assertThat(new String(bytes, 3, 4, Charset.forName("UTF-8")), is("cccc")); } } }