List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:org.apache.cassandra.hadoop.ColumnFamilyRecordReader.java
@Override public boolean next(ByteBuffer key, SortedMap<ByteBuffer, IColumn> value) throws IOException { if (this.nextKeyValue()) { key.clear(); key.put(this.getCurrentKey()); key.rewind();/*from w ww . j a va 2 s . c om*/ value.clear(); value.putAll(this.getCurrentValue()); return true; } return false; }
From source file:xbird.storage.io.RemoteVarSegments.java
private ByteBuffer truncateBuffer(final ByteBuffer buf, final int size) { if (size > buf.capacity()) { _rbufPool.returnObject(buf);/*from w w w .ja v a 2 s. c o m*/ return ByteBuffer.allocate(size); // TODO REVIEWME } else { buf.clear(); buf.limit(size); return buf; } }
From source file:com.gpl_compression.lzo.LzoCompressor.java
/** * Reallocates a direct byte buffer by freeing the old one and allocating * a new one, unless the size is the same, in which case it is simply * cleared and returned./*ww w. jav a2s .c om*/ * * NOTE: this uses unsafe APIs to manually free memory - if anyone else * has a reference to the 'buf' parameter they will likely read random * data or cause a segfault by accessing it. */ private ByteBuffer realloc(ByteBuffer buf, int newSize) { if (buf != null) { if (buf.capacity() == newSize) { // Can use existing buffer buf.clear(); return buf; } try { // Manually free the old buffer using undocumented unsafe APIs. // If this fails, we'll drop the reference and hope GC finds it // eventually. Object cleaner = buf.getClass().getMethod("cleaner").invoke(buf); cleaner.getClass().getMethod("clean").invoke(cleaner); } catch (Exception e) { // Perhaps a non-sun-derived JVM - contributions welcome LOG.warn("Couldn't realloc bytebuffer", e); } } return ByteBuffer.allocateDirect(newSize); }
From source file:com.streamsets.pipeline.lib.generator.wholefile.WholeFileDataGenerator.java
@Override public void write(Record record) throws IOException, DataGeneratorException { validateRecord(record);//from ww w. jav a 2 s .co m FileRef fileRef = record.get(FileRefUtil.FILE_REF_FIELD_PATH).getValueAsFileRef(); int bufferSize = fileRef.getBufferSize(); boolean canUseDirectByteBuffer = fileRef.getSupportedStreamClasses().contains(ReadableByteChannel.class); if (canUseDirectByteBuffer) { //Don't have to close this here, because generate.close will call output stream close WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); //NOSONAR try (ReadableByteChannel readableByteChannel = getReadableStream(fileRef, ReadableByteChannel.class)) { ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize); while ((readableByteChannel.read(buffer)) > 0) { //Flip to use the buffer from 0 to position. buffer.flip(); while (buffer.hasRemaining()) { writableByteChannel.write(buffer); } //Compact the buffer for reuse. buffer.clear(); } } } else { byte[] b = new byte[bufferSize]; try (InputStream stream = getReadableStream(fileRef, InputStream.class)) { IOUtils.copyLarge(stream, outputStream, b); } } }
From source file:acromusashi.kafka.log.producer.WinApacheLogProducer.java
/** * ????//from w w w .j a v a2s . c o m * * @param random * @return ????? * @throws IOException */ private byte[] readToEnd(RandomAccessFile random) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_ALLOCATE_SIZE); while ((random.getChannel().read(buffer)) != -1) { out.write(buffer.array()); buffer.clear(); } return out.toByteArray(); }
From source file:org.cloudata.core.commitlog.pipe.Message.java
public boolean write(SocketChannel channel) throws IOException { if (writingHeaderPhase) { ByteBuffer dupHeaderBuf = duplicate(headerBuf); dupHeaderBuf.position(writtenPosition); dupHeaderBuf.limit(headerBuf.capacity()); channel.write(dupHeaderBuf);/*from w w w . j a va 2 s . c om*/ if (dupHeaderBuf.hasRemaining()) { writtenPosition = dupHeaderBuf.position(); return false; } dupHeaderBuf.clear(); writtenPosition = 0; writingHeaderPhase = false; } ByteBuffer dupBuffer = duplicate(buffer); dupBuffer.position(writtenPosition); dupBuffer.limit(buffer.capacity()); channel.write(dupBuffer); if (dupBuffer.hasRemaining()) { writtenPosition = dupBuffer.position(); return false; } writtenPosition = 0; writingHeaderPhase = true; dupBuffer.clear(); return true; }
From source file:com.cloud.maint.UpgradeManagerImpl.java
public String deployNewAgent(String url) { s_logger.info("Updating agent with binary from " + url); final HttpClient client = new HttpClient(s_httpClientManager); final GetMethod method = new GetMethod(url); int response; File file = null;/* w w w .ja v a 2s . c o m*/ try { response = client.executeMethod(method); if (response != HttpURLConnection.HTTP_OK) { s_logger.warn("Retrieving the agent gives response code: " + response); return "Retrieving the file from " + url + " got response code: " + response; } final InputStream is = method.getResponseBodyAsStream(); file = File.createTempFile("agent-", "-" + Long.toString(new Date().getTime())); file.deleteOnExit(); s_logger.debug("Retrieving new agent into " + file.getAbsolutePath()); final FileOutputStream fos = new FileOutputStream(file); final ByteBuffer buffer = ByteBuffer.allocate(2048); final ReadableByteChannel in = Channels.newChannel(is); final WritableByteChannel out = fos.getChannel(); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } in.close(); out.close(); s_logger.debug("New Agent zip file is now retrieved"); } catch (final HttpException e) { return "Unable to retrieve the file from " + url; } catch (final IOException e) { return "Unable to retrieve the file from " + url; } finally { method.releaseConnection(); } file.delete(); return "File will be deployed."; }
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());/* ww w . j av a 2 s .c om*/ 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:net.ymate.platform.serv.nio.support.NioSession.java
public void read() throws IOException { if (__buffer == null) { __buffer = ByteBufferBuilder.allocate(__eventGroup.bufferSize()); }/* ww w.ja v 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.vxquery.xtest.TestRunner.java
public TestCaseResult run(final TestCase testCase) { TestCaseResult res = new TestCaseResult(testCase); if (opts.verbose) { System.err.println("Starting " + testCase.getXQueryDisplayName()); }/*from ww w . j av a 2 s . c o m*/ long start = System.currentTimeMillis(); try { try { FileInputStream query = new FileInputStream(testCase.getXQueryFile()); if (opts.showQuery) { System.err.println("***Query for " + testCase.getXQueryDisplayName() + ": "); System.err.println(IOUtils.toString(query, "UTF-8")); } VXQueryCompilationListener listener = new VXQueryCompilationListener(opts.showAST, opts.showTET, opts.showOET, opts.showRP); XMLQueryCompiler compiler = new XMLQueryCompiler(listener, new String[] { "nc1" }, opts.frameSize); Reader in = new InputStreamReader(new FileInputStream(testCase.getXQueryFile()), "UTF-8"); CompilerControlBlock ccb = new CompilerControlBlock( new StaticContextImpl(RootStaticContextImpl.INSTANCE), new ResultSetId(testCase.getXQueryDisplayName().hashCode()), testCase.getSourceFileMap()); compiler.compile(testCase.getXQueryDisplayName(), in, ccb, opts.optimizationLevel); JobSpecification spec = compiler.getModule().getHyracksJobSpecification(); in.close(); DynamicContext dCtx = new DynamicContextImpl(compiler.getModule().getModuleContext()); spec.setGlobalJobDataFactory(new VXQueryGlobalDataFactory(dCtx.createFactory())); spec.setMaxReattempts(0); JobId jobId = hcc.startJob(spec, EnumSet.of(JobFlag.PROFILE_RUNTIME)); if (hds == null) { hds = new HyracksDataset(hcc, spec.getFrameSize(), opts.threads); } ByteBuffer buffer = ByteBuffer.allocate(spec.getFrameSize()); IHyracksDatasetReader reader = hds.createReader(jobId, ccb.getResultSetId()); IFrameTupleAccessor frameTupleAccessor = new ResultFrameTupleAccessor(spec.getFrameSize()); buffer.clear(); res.result = ""; while (reader.read(buffer) > 0) { buffer.clear(); res.result += ResultUtils.getStringFromBuffer(buffer, frameTupleAccessor); } res.result.trim(); hcc.waitForCompletion(jobId); } catch (HyracksException e) { Throwable t = e; while (t.getCause() != null) { t = t.getCause(); } Matcher m = EMBEDDED_SYSERROR_PATTERN.matcher(t.getMessage()); if (m.find()) { String eCode = m.group(1); throw new SystemException(ErrorCode.valueOf(eCode), e); } throw e; } } catch (Throwable e) { // Check for nested SystemExceptions. Throwable error = e; while (error != null) { if (error instanceof SystemException) { res.error = error; break; } error = error.getCause(); } // Default if (res.error == null) { res.error = e; } } finally { try { res.compare(); } catch (Exception e) { System.err.println("Framework error"); e.printStackTrace(); } long end = System.currentTimeMillis(); res.time = end - start; } if (opts.showResult) { if (res.result == null) { System.err.println("***Error: "); System.err.println("Message: " + res.error.getMessage()); res.error.printStackTrace(); } else { System.err.println("***Result: "); System.err.println(res.result); } } return res; }