List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:com.facebook.hive.orc.ReaderImpl.java
public ReaderImpl(FileSystem fs, Path path, Configuration conf) throws IOException { try {//from w w w . j a v a 2s. c om 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:org.apache.htrace.impl.PackedBufferManager.java
private void doRecv(SelectionKey sockKey, ByteBuffer response) throws IOException { sockKey.interestOps(SelectionKey.OP_READ); SocketChannel sock = (SocketChannel) sockKey.attachment(); int totalRead = response.remaining(); long startMs = TimeUtil.nowMs(); long remainingMs = conf.ioTimeoutMs; while (remainingMs > 0) { selector.select(remainingMs);//from w ww .j a v a 2s. co m for (SelectionKey key : selector.selectedKeys()) { if (key.isReadable()) { sock.read(response); } } if (response.remaining() == 0) { if (LOG.isTraceEnabled()) { LOG.trace("Received all " + totalRead + " bytes from " + conf.endpointStr); } return; } remainingMs = updateRemainingMs(startMs, conf.ioTimeoutMs); if (LOG.isTraceEnabled()) { LOG.trace("Received " + (totalRead - response.remaining()) + " out of " + totalRead + " bytes from " + conf.endpointStr); } if (remainingMs == 0) { throw new IOException("Attempt to write to " + conf.endpointStr + " timed out after " + TimeUtil.deltaMs(startMs, TimeUtil.nowMs()) + " ms."); } } }
From source file:com.l2jfree.loginserver.network.L2ClientSelectorThread.java
public void printDebug(ByteBuffer buf, L2Client client, int opcode) { report(ErrorMode.INVALID_OPCODE, client, null, null); //if (!Config.PACKET_HANDLER_DEBUG) // return;/* www . ja v a 2 s . co m*/ StringBuilder sb = new StringBuilder("Unknown Packet: "); sb.append("0x").append(Integer.toHexString(opcode)); sb.append(", Client: ").append(client); _log.info(sb); byte[] array = new byte[buf.remaining()]; buf.get(array); for (String line : StringUtils.split(HexUtil.printData(array), "\n")) _log.info(line); }
From source file:net.ymate.platform.serv.nio.support.NioSession.java
public void read() throws IOException { if (__buffer == null) { __buffer = ByteBufferBuilder.allocate(__eventGroup.bufferSize()); }// w w w .j av a 2 s . c o m ByteBuffer _data = ByteBuffer.allocate(__eventGroup.bufferSize()); int _len = 0; while ((_len = __doChannelRead(_data)) > 0) { _data.flip(); __buffer.append(_data.array(), _data.position(), _data.remaining()); _data.clear(); } if (_len < 0) { close(); return; } ByteBufferBuilder _copiedBuffer = __buffer.duplicate().flip(); while (true) { _copiedBuffer.mark(); Object _message = null; if (_copiedBuffer.remaining() > 0) { _message = __eventGroup.codec().decode(_copiedBuffer); } else { _message = null; } if (_message == null) { _copiedBuffer.reset(); __doBufferReset(_copiedBuffer); break; } else { final Object _copiedObj = _message; __eventGroup.executorService().submit(new Runnable() { public void run() { try { __eventGroup.listener().onMessageReceived(_copiedObj, NioSession.this); } catch (IOException e) { try { __eventGroup.listener().onExceptionCaught(e, NioSession.this); } catch (IOException ex) { try { close(); } catch (Throwable exx) { _LOG.error(exx.getMessage(), RuntimeUtils.unwrapThrow(exx)); } } } } }); } } }
From source file:org.apache.tajo.master.exec.NonForwardQueryResultFileScanner.java
@Override public SerializedResultSet nextRowBlock(int fetchRowNum) throws IOException { try {//from w ww. ja v a2 s.com final SerializedResultSet.Builder resultSetBuilder = SerializedResultSet.newBuilder(); resultSetBuilder.setSchema(scanNode.getOutSchema().getProto()); resultSetBuilder.setRows(0); if (isStopped) return resultSetBuilder.build(); if (nextFetch == null) { nextFetch = fetchNextRowBlock(fetchRowNum); } MemoryRowBlock rowBlock = nextFetch.get(); if (rowBlock.rows() > 0) { resultSetBuilder.setRows(rowBlock.rows()); MemoryBlock memoryBlock = rowBlock.getMemory(); if (codecType.isPresent()) { byte[] uncompressedBytes = new byte[memoryBlock.readableBytes()]; memoryBlock.getBuffer().getBytes(0, uncompressedBytes); byte[] compressedBytes = CompressionUtil.compress(codecType.get(), uncompressedBytes); resultSetBuilder.setDecompressedLength(uncompressedBytes.length); resultSetBuilder.setDecompressCodec(codecType.get()); resultSetBuilder.setSerializedTuples(ByteString.copyFrom(compressedBytes)); } else { ByteBuffer uncompressed = memoryBlock.getBuffer().nioBuffer(0, memoryBlock.readableBytes()); resultSetBuilder.setDecompressedLength(uncompressed.remaining()); resultSetBuilder.setSerializedTuples(ByteString.copyFrom(uncompressed)); } } // pre-fetch if (!eof) { nextFetch = fetchNextRowBlock(fetchRowNum); } else { close(); } return resultSetBuilder.build(); } catch (Throwable t) { close(); throw new TajoInternalError(t.getCause()); } }
From source file:com.msopentech.thali.relay.RelayWebServer.java
@Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); String queryString = session.getQueryParameterString(); String path = session.getUri(); Map<String, String> headers = session.getHeaders(); LOG.info("URI + Query: " + path + (queryString == null ? "" : "?" + queryString)); LOG.info("METHOD: " + method.toString()); LOG.info("ORIGIN: " + headers.get("origin")); // Handle an OPTIONS request without relaying anything for now // TODO: Relay OPTIONS properly, but manage the CORS aspects so // we don't introduce dependencies on couch CORS configuration if (method.name().equalsIgnoreCase("OPTIONS")) { Response optionsResponse = new Response("OK"); AppendCorsHeaders(optionsResponse, headers); optionsResponse.setStatus(Response.Status.OK); return optionsResponse; }/* w w w . j a v a 2 s. c om*/ // Handle request for local HTTP Key URL // TODO: Temporary fake values, need to build hook to handle magic URLs and generate proper HTTPKey if (path.equalsIgnoreCase("/_relayutility/localhttpkeys")) { ObjectMapper mapper = new ObjectMapper(); String responseBody = null; try { responseBody = mapper.writeValueAsString(httpKeyTypes); } catch (JsonProcessingException e) { throw new RuntimeException("Could not generate localhttpkeys output", e); } Response httpKeyResponse = new Response(responseBody); AppendCorsHeaders(httpKeyResponse, headers); httpKeyResponse.setMimeType("application/json"); httpKeyResponse.setStatus(Response.Status.OK); return httpKeyResponse; } // Get the body of the request if appropriate byte[] requestBody = new byte[0]; if (method.equals(Method.PUT) || method.equals(Method.POST)) { try { ByteBuffer bodyBuffer = ((HTTPSession) session).getBody(); if (bodyBuffer != null) { requestBody = new byte[bodyBuffer.remaining()]; bodyBuffer.get(requestBody); } } catch (Exception e) { e.printStackTrace(); return GenerateErrorResponse(e.getMessage()); } } // Make a new request which we will prepare for relaying to TDH BasicHttpEntityEnclosingRequest basicHttpRequest = null; try { basicHttpRequest = buildRelayRequest(method, path, queryString, headers, requestBody); } catch (UnsupportedEncodingException e) { String message = "Unable to translate body to new request.\n" + ExceptionUtils.getStackTrace(e); return GenerateErrorResponse(message); } catch (URISyntaxException e) { return GenerateErrorResponse( "Unable to generate the URL for the local TDH.\n" + ExceptionUtils.getStackTrace(e)); } // Actually make the relayed call HttpResponse tdhResponse = null; InputStream tdhResponseContent = null; Response clientResponse = null; try { LOG.info("Relaying call to TDH: " + httpHost.toURI()); tdhResponse = httpClient.execute(httpHost, basicHttpRequest); tdhResponseContent = tdhResponse.getEntity().getContent(); // Create response and set status and body // default the MIME_TYPE for now and we'll set it later when we enumerate the headers if (tdhResponse != null) { // This is horrible awful evil code to deal with CouchBase note properly sending CouchDB responses // for some errors. We must fix this in CouchBase - https://github.com/thaliproject/thali/issues/30 String responseBodyString = null; if (tdhResponse.containsHeader("content-type") && tdhResponse.getFirstHeader("content-type") .getValue().equalsIgnoreCase("application/json")) { if (tdhResponse.getStatusLine().getStatusCode() == 404) { responseBodyString = "{\"error\":\"not_found\"}"; } if (tdhResponse.getStatusLine().getStatusCode() == 412) { responseBodyString = "{\"error\":\"missing_id\"}"; } } clientResponse = new Response(new RelayStatus(tdhResponse.getStatusLine()), NanoHTTPD.MIME_PLAINTEXT, responseBodyString != null ? IOUtils.toInputStream(responseBodyString) : IOUtils.toBufferedInputStream(tdhResponseContent)); // If there is a response body we want to send it chunked to enable streaming clientResponse.setChunkedTransfer(true); } } catch (IOException e) { String message = "Reading response failed!\n" + ExceptionUtils.getStackTrace(e); return GenerateErrorResponse(message); } finally { // Make sure the read stream is closed so we don't exhaust our pool if (tdhResponseContent != null) try { tdhResponseContent.close(); } catch (IOException e) { LOG.error(e.getMessage()); } } // Prep all headers for our final response AppendCorsHeaders(clientResponse, headers); copyHeadersToResponse(clientResponse, tdhResponse.getAllHeaders()); return clientResponse; }
From source file:org.apache.hadoop.crypto.OpensslCipher.java
/** * Continues a multiple-part encryption or decryption operation. The data * is encrypted or decrypted, depending on how this cipher was initialized. * <p/>/*from ww w .j ava2 s .c o m*/ * * All <code>input.remaining()</code> bytes starting at * <code>input.position()</code> are processed. The result is stored in * the output buffer. * <p/> * * Upon return, the input buffer's position will be equal to its limit; * its limit will not have changed. The output buffer's position will have * advanced by n, when n is the value returned by this method; the output * buffer's limit will not have changed. * <p/> * * If <code>output.remaining()</code> bytes are insufficient to hold the * result, a <code>ShortBufferException</code> is thrown. * * @param input the input ByteBuffer * @param output the output ByteBuffer * @return int number of bytes stored in <code>output</code> * @throws ShortBufferException if there is insufficient space in the * output buffer */ public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); int len = update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining()); input.position(input.limit()); output.position(output.position() + len); return len; }
From source file:android.pim.vcard.VCardEntryConstructor.java
private String encodeString(String originalString, String charsetForDecodedBytes) { if (mInputCharset.equalsIgnoreCase(charsetForDecodedBytes)) { return originalString; }/*from www .j a v a 2s. c o m*/ Charset charset = Charset.forName(mInputCharset); ByteBuffer byteBuffer = charset.encode(originalString); // byteBuffer.array() "may" return byte array which is larger than // byteBuffer.remaining(). Here, we keep on the safe side. byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes); try { return new String(bytes, charsetForDecodedBytes); } catch (UnsupportedEncodingException e) { Log.e(LOG_TAG, "Failed to encode: charset=" + charsetForDecodedBytes); return null; } }
From source file:com.intel.chimera.codec.OpensslCipher.java
/** * Continues a multiple-part encryption or decryption operation. The data * is encrypted or decrypted, depending on how this cipher was initialized. * <p/>/*from w w w.j ava 2 s. c om*/ * * All <code>input.remaining()</code> bytes starting at * <code>input.position()</code> are processed. The result is stored in * the output buffer. * <p/> * * Upon return, the input buffer's position will be equal to its limit; * its limit will not have changed. The output buffer's position will have * advanced by n, when n is the value returned by this method; the output * buffer's limit will not have changed. * <p/> * * If <code>output.remaining()</code> bytes are insufficient to hold the * result, a <code>ShortBufferException</code> is thrown. * * @param input the input ByteBuffer * @param output the output ByteBuffer * @return int number of bytes stored in <code>output</code> * @throws ShortBufferException if there is insufficient space in the * output buffer */ public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); int len = OpensslCipherNative.update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining()); input.position(input.limit()); output.position(output.position() + len); return len; }
From source file:org.apache.cassandra.db.context.CounterContextTest.java
@Test public void testMerge() { // note: local counts aggregated; remote counts are reconciled (i.e. take max) ContextState left = ContextState.allocate(4, 1); left.writeElement(NodeId.fromInt(1), 1L, 1L); left.writeElement(NodeId.fromInt(2), 2L, 2L); left.writeElement(NodeId.fromInt(4), 6L, 3L); left.writeElement(NodeId.getLocalId(), 7L, 3L, true); ContextState right = ContextState.allocate(3, 1); right.writeElement(NodeId.fromInt(4), 4L, 4L); right.writeElement(NodeId.fromInt(5), 5L, 5L); right.writeElement(NodeId.getLocalId(), 2L, 9L, true); ByteBuffer merged = cc.merge(left.context, right.context); int hd = 4;/*from www . j a v a 2 s.com*/ assertEquals(hd + 5 * stepLength, merged.remaining()); // local node id's counts are aggregated assert Util.equalsNodeId(NodeId.getLocalId(), merged, hd + 4 * stepLength); assertEquals(9L, merged.getLong(hd + 4 * stepLength + idLength)); assertEquals(12L, merged.getLong(hd + 4 * stepLength + idLength + clockLength)); // remote node id counts are reconciled (i.e. take max) assert Util.equalsNodeId(NodeId.fromInt(4), merged, hd + 2 * stepLength); assertEquals(6L, merged.getLong(hd + 2 * stepLength + idLength)); assertEquals(3L, merged.getLong(hd + 2 * stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(5), merged, hd + 3 * stepLength); assertEquals(5L, merged.getLong(hd + 3 * stepLength + idLength)); assertEquals(5L, merged.getLong(hd + 3 * stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(2), merged, hd + 1 * stepLength); assertEquals(2L, merged.getLong(hd + 1 * stepLength + idLength)); assertEquals(2L, merged.getLong(hd + 1 * stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(1), merged, hd + 0 * stepLength); assertEquals(1L, merged.getLong(hd + 0 * stepLength + idLength)); assertEquals(1L, merged.getLong(hd + 0 * stepLength + idLength + clockLength)); }