List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:org.apache.bookkeeper.bookie.LedgerStorageCheckpointTest.java
private LogMark readLastMarkFile(File lastMarkFile) throws IOException { byte buff[] = new byte[16]; ByteBuffer bb = ByteBuffer.wrap(buff); LogMark rolledLogMark = new LogMark(); FileInputStream fis = new FileInputStream(lastMarkFile); int bytesRead = fis.read(buff); fis.close();/*from w ww .j a v a 2 s. c o m*/ if (bytesRead != 16) { throw new IOException( "Couldn't read enough bytes from lastMark." + " Wanted " + 16 + ", got " + bytesRead); } bb.clear(); rolledLogMark.readLogMark(bb); return rolledLogMark; }
From source file:org.alfresco.contentstore.patch.PatchServiceImpl.java
private void updatePatchDocument(PatchDocument patchDocument, NodeChecksums checksums, Reader reader) throws IOException { ByteBuffer data = ByteBuffer.allocate(blockSize * 20); int blockSize = checksums.getBlockSize(); int i = 0;// www.j a va 2 s. c o m Adler32 adlerInfo = new Adler32(hasher); int lastMatchIndex = 1; // starts at 1 ByteBuffer currentPatch = ByteBuffer.allocate(5000000); // TODO int x = 0; for (;;) { if (x == 0 || i >= data.limit()) { data.clear(); i = 0; int numRead = reader.read(data); if (numRead <= 0) { break; } data.flip(); x += numRead; } int chunkSize = 0; // determine the size of the next data chuck to evaluate. Default to // blockSize, but clamp to end of data if ((i + blockSize) > data.limit()) { chunkSize = data.limit() - i; adlerInfo.reset(); // need to reset this because the rolling // checksum doesn't work correctly on a final // non-aligned block } else { chunkSize = blockSize; } int end = i + chunkSize - 1; int matchedBlockIndex = adlerInfo.checkMatch(lastMatchIndex, checksums, data, i, end); if (matchedBlockIndex != -1) { // try // { // String y = hasher.md5(data, i, end); // System.out.println("y = " + y + ", x = " + x + ", i = " + i + ", end = " + end); // } // catch (NoSuchAlgorithmException e) // { // // TODO Auto-generated catch block // e.printStackTrace(); // } // if we have a match, do the following: // 1) add the matched block index to our tracking buffer // 2) check to see if there's a current patch. If so, add it to // the patch document. // 3) jump forward blockSize bytes and continue patchDocument.addMatchedBlock(matchedBlockIndex); if (currentPatch.position() > 0) { // there are outstanding patches, add them to the list // create the patch and append it to the patches buffer currentPatch.flip(); int size = currentPatch.limit(); byte[] dst = new byte[size]; currentPatch.get(dst, 0, size); Patch patch = new Patch(lastMatchIndex, size, dst); patchDocument.addPatch(patch); currentPatch.clear(); } lastMatchIndex = matchedBlockIndex; i += chunkSize; adlerInfo.reset(); } else { // while we don't have a block match, append bytes to the // current patch if (currentPatch.position() >= currentPatch.limit()) { // System.out.println("count=" + (x + i)); // System.out.println("count1=" + currentPatch.position() + ", " + currentPatch.limit()); // System.out.println(matchedBlockIndexes); // System.out.println(patches); } currentPatch.put(data.get(i)); i++; } } // end for each byte in the data if (currentPatch.position() > 0) { currentPatch.flip(); int size = currentPatch.limit(); byte[] dst = new byte[size]; currentPatch.get(dst, 0, size); Patch patch = new Patch(lastMatchIndex, size, dst); patchDocument.addPatch(patch); } }
From source file:com.carreygroup.JARVIS.Demon.java
public boolean Send(Packet p) throws IOException { boolean ret = false; if ((Ethnet_Mode == Ethnet.TCP) || (Ethnet_Mode == Ethnet.P2P)) { ByteBuffer bytebufOut = ByteBuffer.allocate(Ethnet.BUFFER_SIZE); bytebufOut = ByteBuffer.wrap(p.toByteArray()); try {/*from w w w. j ava2 s .com*/ mPrintWriterClient.print(new String(bytebufOut.array()));// mPrintWriterClient.flush(); ret = true; } catch (Exception e) { notifyDisconnected(); throw new IOException(""); } bytebufOut.flip(); bytebufOut.clear(); } if (Ethnet_Mode == Ethnet.UDP) { byte[] buff = p.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length, mAddress); mSendPSocket.send(packet);// ret = true; } return ret; }
From source file:org.celstec.arlearn2.upload.BlobStoreServlet.java
private BlobKey storeBlob(String contentType, String fileName, InputStream stream) throws IOException { FileService fileService = FileServiceFactory.getFileService(); AppEngineFile file = fileService.createNewBlobFile(contentType, fileName); boolean lock = true; FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); ByteBuffer buf = ByteBuffer.allocateDirect(10); byte[] bytes = new byte[1024]; int count = 0; int index = 0; // Continue writing bytes until there are no more while (count >= 0) { if (index == count) { count = stream.read(bytes);// w ww .j a v a 2 s.c om index = 0; } // Fill ByteBuffer while (index < count && buf.hasRemaining()) { buf.put(bytes[index++]); } // Set the limit to the current position and the // position to 0 // making the new bytes visible for write() buf.flip(); // Write the bytes to the channel int numWritten = writeChannel.write(buf); // Check if all bytes were written if (buf.hasRemaining()) { buf.compact(); } else { buf.clear(); } } writeChannel.closeFinally(); return fileService.getBlobKey(file); }
From source file:org.apache.hadoop.mapred.FadvisedFileRegion.java
/** * This method transfers data using local buffer. It transfers data from * a disk to a local buffer in memory, and then it transfers data from the * buffer to the target. This is used only if transferTo is disallowed in * the configuration file. super.TransferTo does not perform well on Windows * due to a small IO request generated. customShuffleTransfer can control * the size of the IO requests by changing the size of the intermediate * buffer./* ww w . ja v a 2 s. c om*/ */ @VisibleForTesting long customShuffleTransfer(WritableByteChannel target, long position) throws IOException { long actualCount = this.count - position; if (actualCount < 0 || position < 0) { throw new IllegalArgumentException( "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')'); } if (actualCount == 0) { return 0L; } long trans = actualCount; int readSize; ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize); while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) { //adjust counters and buffer limit if (readSize < trans) { trans -= readSize; position += readSize; byteBuffer.flip(); } else { //We can read more than we need if the actualCount is not multiple //of the byteBuffer size and file is big enough. In that case we cannot //use flip method but we need to set buffer limit manually to trans. byteBuffer.limit((int) trans); byteBuffer.position(0); position += trans; trans = 0; } //write data to the target while (byteBuffer.hasRemaining()) { target.write(byteBuffer); } byteBuffer.clear(); } return actualCount - trans; }
From source file:org.celstec.arlearn2.upload.BlobStoreServletIncremental.java
private AppEngineFile storeBlob(String contentType, String fileName, InputStream stream, boolean last, String serverPath) throws IOException { AppEngineFile file;/*from w ww . j a v a 2 s . c om*/ if (serverPath == null) { file = fileService.createNewBlobFile(contentType, fileName); } else { file = new AppEngineFile(serverPath); } // boolean lock = true; log.warning("last is" + last + "file fullpath " + file.getFullPath()); FileWriteChannel writeChannel = fileService.openWriteChannel(file, last); ByteBuffer buf = ByteBuffer.allocateDirect(10); byte[] bytes = new byte[1024]; int count = 0; int index = 0; // Continue writing bytes until there are no more while (count >= 0) { if (index == count) { count = stream.read(bytes); index = 0; } // Fill ByteBuffer while (index < count && buf.hasRemaining()) { buf.put(bytes[index++]); } // Set the limit to the current position and the // position to 0 // making the new bytes visible for write() buf.flip(); // Write the bytes to the channel int numWritten = writeChannel.write(buf); // Check if all bytes were written if (buf.hasRemaining()) { buf.compact(); } else { buf.clear(); } } writeChannel.close(); if (last) writeChannel.closeFinally(); return file; // return fileService.getBlobKey(file); }
From source file:org.apache.tajo.pullserver.FadvisedFileRegion.java
/** * This method transfers data using local buffer. It transfers data from * a disk to a local buffer in memory, and then it transfers data from the * buffer to the target. This is used only if transferTo is disallowed in * the configuration file. super.TransferTo does not perform well on Windows * due to a small IO request generated. customShuffleTransfer can control * the size of the IO requests by changing the size of the intermediate * buffer.//from w ww . j ava 2s . c o m */ @VisibleForTesting long customShuffleTransfer(WritableByteChannel target, long position) throws IOException { long actualCount = this.count - position; if (actualCount < 0 || position < 0) { throw new IllegalArgumentException( "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')'); } if (actualCount == 0) { return 0L; } long trans = actualCount; int readSize; ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize); while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) { //adjust counters and buffer limit if (readSize < trans) { trans -= readSize; position += readSize; byteBuffer.flip(); } else { //We can read more than we need if the actualCount is not multiple //of the byteBuffer size and file is big enough. In that case we cannot //use flip method but we need to set buffer limit manually to trans. byteBuffer.limit((int) trans); byteBuffer.position(0); position += trans; trans = 0; } //write data to the target while (byteBuffer.hasRemaining()) { target.write(byteBuffer); } byteBuffer.clear(); } return actualCount - trans; }
From source file:org.apache.nifi.processor.util.listen.dispatcher.DatagramChannelDispatcher.java
@Override public void run() { final ByteBuffer buffer = bufferPool.poll(); while (!stopped) { try {/*from w w w . ja v a 2s. c om*/ int selected = selector.select(); // if stopped the selector could already be closed which would result in a ClosedSelectorException if (selected > 0 && !stopped) { Iterator<SelectionKey> selectorKeys = selector.selectedKeys().iterator(); // if stopped we don't want to modify the keys because close() may still be in progress while (selectorKeys.hasNext() && !stopped) { SelectionKey key = selectorKeys.next(); selectorKeys.remove(); if (!key.isValid()) { continue; } DatagramChannel channel = (DatagramChannel) key.channel(); SocketAddress socketAddress; buffer.clear(); while (!stopped && (socketAddress = channel.receive(buffer)) != null) { String sender = ""; if (socketAddress instanceof InetSocketAddress) { sender = ((InetSocketAddress) socketAddress).getAddress().toString(); } // create a byte array from the buffer buffer.flip(); byte bytes[] = new byte[buffer.limit()]; buffer.get(bytes, 0, buffer.limit()); final Map<String, String> metadata = EventFactoryUtil.createMapWithSender(sender); final E event = eventFactory.create(bytes, metadata, null); events.offer(event); buffer.clear(); } } } } catch (InterruptedException e) { stopped = true; Thread.currentThread().interrupt(); } catch (IOException e) { logger.error("Error reading from DatagramChannel", e); } } if (buffer != null) { try { bufferPool.put(buffer); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
From source file:org.asynchttpclient.providers.apache.ApacheAsyncHttpProvider.java
private HttpMethodBase createMethod(HttpClient client, Request request) throws IOException, FileNotFoundException { String methodName = request.getMethod(); HttpMethodBase method = null;/*from w ww . j a va2 s.co m*/ if (methodName.equalsIgnoreCase("POST") || methodName.equalsIgnoreCase("PUT")) { EntityEnclosingMethod post = methodName.equalsIgnoreCase("POST") ? new PostMethod(request.getUrl()) : new PutMethod(request.getUrl()); String bodyCharset = request.getBodyEncoding() == null ? DEFAULT_CHARSET : request.getBodyEncoding(); post.getParams().setContentCharset("ISO-8859-1"); if (request.getByteData() != null) { post.setRequestEntity(new ByteArrayRequestEntity(request.getByteData())); post.setRequestHeader("Content-Length", String.valueOf(request.getByteData().length)); } else if (request.getStringData() != null) { post.setRequestEntity(new StringRequestEntity(request.getStringData(), "text/xml", bodyCharset)); post.setRequestHeader("Content-Length", String.valueOf(request.getStringData().getBytes(bodyCharset).length)); } else if (request.getStreamData() != null) { InputStreamRequestEntity r = new InputStreamRequestEntity(request.getStreamData()); post.setRequestEntity(r); post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength())); } else if (request.getParams() != null) { StringBuilder sb = new StringBuilder(); for (final Map.Entry<String, List<String>> paramEntry : request.getParams()) { final String key = paramEntry.getKey(); for (final String value : paramEntry.getValue()) { if (sb.length() > 0) { sb.append("&"); } UTF8UrlEncoder.appendEncoded(sb, key); sb.append("="); UTF8UrlEncoder.appendEncoded(sb, value); } } post.setRequestHeader("Content-Length", String.valueOf(sb.length())); post.setRequestEntity(new StringRequestEntity(sb.toString(), "text/xml", "ISO-8859-1")); if (!request.getHeaders().containsKey("Content-Type")) { post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } } else if (request.getParts() != null) { MultipartRequestEntity mre = createMultipartRequestEntity(bodyCharset, request.getParts(), post.getParams()); post.setRequestEntity(mre); post.setRequestHeader("Content-Type", mre.getContentType()); post.setRequestHeader("Content-Length", String.valueOf(mre.getContentLength())); } else if (request.getEntityWriter() != null) { post.setRequestEntity(new EntityWriterRequestEntity(request.getEntityWriter(), computeAndSetContentLength(request, post))); } else if (request.getFile() != null) { File file = request.getFile(); if (!file.isFile()) { throw new IOException( String.format(Thread.currentThread() + "File %s is not a file or doesn't exist", file.getAbsolutePath())); } post.setRequestHeader("Content-Length", String.valueOf(file.length())); FileInputStream fis = new FileInputStream(file); try { InputStreamRequestEntity r = new InputStreamRequestEntity(fis); post.setRequestEntity(r); post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength())); } finally { fis.close(); } } else if (request.getBodyGenerator() != null) { Body body = request.getBodyGenerator().createBody(); try { int length = (int) body.getContentLength(); if (length < 0) { length = (int) request.getContentLength(); } // TODO: This is suboptimal if (length >= 0) { post.setRequestHeader("Content-Length", String.valueOf(length)); // This is totally sub optimal byte[] bytes = new byte[length]; ByteBuffer buffer = ByteBuffer.wrap(bytes); for (;;) { buffer.clear(); if (body.read(buffer) < 0) { break; } } post.setRequestEntity(new ByteArrayRequestEntity(bytes)); } } finally { try { body.close(); } catch (IOException e) { logger.warn("Failed to close request body: {}", e.getMessage(), e); } } } String expect = request.getHeaders().getFirstValue("Expect"); if (expect != null && expect.equalsIgnoreCase("100-Continue")) { post.setUseExpectHeader(true); } method = post; } else if (methodName.equalsIgnoreCase("DELETE")) { method = new DeleteMethod(request.getUrl()); } else if (methodName.equalsIgnoreCase("HEAD")) { method = new HeadMethod(request.getUrl()); } else if (methodName.equalsIgnoreCase("GET")) { method = new GetMethod(request.getUrl()); } else if (methodName.equalsIgnoreCase("OPTIONS")) { method = new OptionsMethod(request.getUrl()); } else { throw new IllegalStateException(String.format("Invalid Method", methodName)); } ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request); if (proxyServer != null) { if (proxyServer.getPrincipal() != null) { Credentials defaultcreds = new UsernamePasswordCredentials(proxyServer.getPrincipal(), proxyServer.getPassword()); client.getState().setProxyCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds); } ProxyHost proxyHost = proxyServer == null ? null : new ProxyHost(proxyServer.getHost(), proxyServer.getPort()); client.getHostConfiguration().setProxyHost(proxyHost); } if (request.getLocalAddress() != null) { client.getHostConfiguration().setLocalAddress(request.getLocalAddress()); } method.setFollowRedirects(false); Collection<Cookie> cookies = request.getCookies(); if (isNonEmpty(cookies)) { method.setRequestHeader("Cookie", AsyncHttpProviderUtils.encodeCookies(request.getCookies())); } if (request.getHeaders() != null) { for (String name : request.getHeaders().keySet()) { if (!"host".equalsIgnoreCase(name)) { for (String value : request.getHeaders().get(name)) { method.setRequestHeader(name, value); } } } } String ua = request.getHeaders().getFirstValue("User-Agent"); if (ua != null) { method.setRequestHeader("User-Agent", ua); } else if (config.getUserAgent() != null) { method.setRequestHeader("User-Agent", config.getUserAgent()); } else { method.setRequestHeader("User-Agent", AsyncHttpProviderUtils.constructUserAgent(ApacheAsyncHttpProvider.class, config)); } if (config.isCompressionEnabled()) { Header acceptableEncodingHeader = method.getRequestHeader("Accept-Encoding"); if (acceptableEncodingHeader != null) { String acceptableEncodings = acceptableEncodingHeader.getValue(); if (acceptableEncodings.indexOf("gzip") == -1) { StringBuilder buf = new StringBuilder(acceptableEncodings); if (buf.length() > 1) { buf.append(","); } buf.append("gzip"); method.setRequestHeader("Accept-Encoding", buf.toString()); } } else { method.setRequestHeader("Accept-Encoding", "gzip"); } } if (request.getVirtualHost() != null) { String vs = request.getVirtualHost(); int index = vs.indexOf(":"); if (index > 0) { vs = vs.substring(0, index); } method.getParams().setVirtualHost(vs); } return method; }
From source file:freed.cam.apis.camera2.modules.PictureModuleApi2.java
@NonNull private void process_jpeg(Image image, File file) { Log.d(TAG, "Create JPEG"); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes);/*from www. ja v a 2s . co m*/ saveJpeg(file, bytes); image.close(); buffer.clear(); image = null; }