List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:org.apache.hadoop.hbase.io.ByteBufferPool.java
/** * @return One free ByteBuffer from the pool. If no free ByteBuffer and we have not reached the * maximum pool size, it will create a new one and return. In case of max pool size also * reached, will return null. When pool returned a ByteBuffer, make sure to return it back * to pool after use.//ww w . jav a 2 s .c o m * @see #putbackBuffer(ByteBuffer) */ public ByteBuffer getBuffer() { ByteBuffer bb = buffers.poll(); if (bb != null) { // Clear sets limit == capacity. Position == 0. bb.clear(); return bb; } while (true) { int c = this.count.intValue(); if (c >= this.maxPoolSize) { if (maxPoolSizeInfoLevelLogged) { if (LOG.isDebugEnabled()) { LOG.debug("Pool already reached its max capacity : " + this.maxPoolSize + " and no free buffers now. Consider increasing the value for '" + MAX_POOL_SIZE_KEY + "' ?"); } } else { LOG.info("Pool already reached its max capacity : " + this.maxPoolSize + " and no free buffers now. Consider increasing the value for '" + MAX_POOL_SIZE_KEY + "' ?"); maxPoolSizeInfoLevelLogged = true; } return null; } if (!this.count.compareAndSet(c, c + 1)) { continue; } if (LOG.isTraceEnabled()) { LOG.trace("Creating a new offheap ByteBuffer of size: " + this.bufferSize); } return this.directByteBuffer ? ByteBuffer.allocateDirect(this.bufferSize) : ByteBuffer.allocate(this.bufferSize); } }
From source file:org.cloudata.core.commitlog.pipe.BufferPool.java
public void returnBuffer(ByteBuffer[] bufferArray) { synchronized (bufferMap) { for (ByteBuffer buf : bufferArray) { buf.clear(); TreeSet<PoolEntry> entrySet = bufferMap.get(buf.capacity()); if (entrySet == null) { entrySet = new TreeSet<PoolEntry>(); bufferMap.put(buf.capacity(), entrySet); }//from w ww . jav a2 s .c om entrySet.add(new PoolEntry(buf)); poolMonitor.increaseBuffered(buf.capacity()); } } }
From source file:com.turbospaces.serialization.PropertiesSerializer.java
@Override public Object readID(final ByteBuffer buffer) { final CachedSerializationProperty idProperty = cachedProperties[BO.getIdIndex()]; Object id = DecoratedKryo.readPropertyValue(kryo, idProperty, buffer); buffer.clear(); return id;//from ww w . j ava2 s. c o m }
From source file:org.apache.nifi.processors.standard.util.BaseStrictSyslog5424ParserTest.java
@Test public void testRFC5424WithVersion() { final String pri = "34"; final String version = "1"; final String stamp = "2003-10-11T22:14:15.003Z"; final String host = "mymachine.example.com"; final String appName = "su"; final String procId = "-"; final String msgId = "ID17"; final String structuredData = "-"; final String body = "BOM'su root' failed for lonvick on /dev/pts/8"; final String message = "<" + pri + ">" + version + " " + stamp + " " + host + " " + appName + " " + procId + " " + msgId + " " + "-" + " " + body; final byte[] bytes = message.getBytes(CHARSET); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.clear(); buffer.put(bytes);// www .j a va 2s . co m final Syslog5424Event event = parser.parseEvent(buffer); Assert.assertNotNull(event); Assert.assertTrue(event.isValid()); Assert.assertFalse(event.getFieldMap().isEmpty()); Map<String, String> fieldMap = event.getFieldMap(); Assert.assertEquals(pri, fieldMap.get(SyslogAttributes.PRIORITY.key())); Assert.assertEquals("2", fieldMap.get(SyslogAttributes.SEVERITY.key())); Assert.assertEquals("4", fieldMap.get(SyslogAttributes.FACILITY.key())); Assert.assertEquals(version, fieldMap.get(SyslogAttributes.VERSION.key())); Assert.assertEquals(stamp, fieldMap.get(SyslogAttributes.TIMESTAMP.key())); Assert.assertEquals(host, fieldMap.get(SyslogAttributes.HOSTNAME.key())); Assert.assertEquals(appName, fieldMap.get(Syslog5424Attributes.APP_NAME.key())); validateForPolicy(procId, fieldMap.get(Syslog5424Attributes.PROCID.key())); Assert.assertEquals(msgId, fieldMap.get(Syslog5424Attributes.MESSAGEID.key())); Pattern structuredPattern = new StrictSyslog5424Parser.NifiKeyProvider() .getStructuredElementIdParamNamePattern(); fieldMap.forEach((key, value) -> { if (!StringUtils.isBlank(value)) { Assert.assertFalse(structuredPattern.matcher(value).matches()); } }); Assert.assertEquals(body, fieldMap.get(SyslogAttributes.BODY.key())); Assert.assertEquals(message, event.getFullMessage()); Assert.assertNull(event.getSender()); }
From source file:net.jradius.freeradius.FreeRadiusListener.java
public JRadiusEvent parseRequest(ListenerRequest listenerRequest, ByteBuffer notUsed, InputStream in) throws Exception { FreeRadiusRequest request = (FreeRadiusRequest) requestObjectPool.borrowObject(); request.setBorrowedFromPool(requestObjectPool); int totalLength = (int) (RadiusFormat.readUnsignedInt(in) - 4); int readOffset = 0; ByteBuffer buffer = request.buffer_in; if (totalLength < 0 || totalLength > buffer.capacity()) { return null; }/*from www . j av a2 s . com*/ buffer.clear(); byte[] payload = buffer.array(); while (readOffset < totalLength) { int result = in.read(payload, readOffset, totalLength - readOffset); if (result < 0) return null; readOffset += result; } buffer.limit(totalLength); long nameLength = RadiusFormat.getUnsignedInt(buffer); if (nameLength < 0 || nameLength > 1024) { throw new RadiusException("KeepAlive rlm_jradius connection has been closed"); } byte[] nameBytes = new byte[(int) nameLength]; buffer.get(nameBytes); int messageType = RadiusFormat.getUnsignedByte(buffer); int packetCount = RadiusFormat.getUnsignedByte(buffer); RadiusPacket rp[] = PacketFactory.parse(buffer, packetCount); long length = RadiusFormat.getUnsignedInt(buffer); if (length > buffer.remaining()) { throw new RadiusException("bad length"); } AttributeList configItems = new AttributeList(); format.unpackAttributes(configItems, buffer, (int) length, true); request.setConfigItems(configItems); request.setSender(new String(nameBytes)); request.setType(messageType); request.setPackets(rp); return request; }
From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java
@Test public void test_primaryStage() throws Exception { ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("Hello World".getBytes()); buffer.flip();//from ww w . j a v a 2 s . c o m staging.writePrimaryStaging(buffer, "Hello World".getBytes().length); buffer.clear(); staging.getPrimaryStage().read(buffer); Assert.assertEquals("Hello World".getBytes().length, staging.getPrimaryStageSize()); Assert.assertEquals("Hello World", new String(buffer.array(), 0, buffer.position())); staging.resetPrimaryStage(); Assert.assertEquals(0, staging.getPrimaryStageSize()); }
From source file:com.github.neoio.net.message.staging.memory.TestMemoryMessageStaging.java
@Test public void test_tempRead() { ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("Hello World".getBytes()); buffer.rewind();// w w w . j a v a2s .c o m staging.writeTempReadBytes(buffer); Assert.assertTrue(staging.hasTempReadBytes()); buffer.clear(); staging.readTempReadBytes(buffer); Assert.assertEquals("Hello World", new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length))); staging.resetTempReadBytes(); Assert.assertFalse(staging.hasTempReadBytes()); }
From source file:com.github.neoio.net.message.staging.memory.TestMemoryMessageStaging.java
@Test public void test_tempWrite() { ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("Hello World".getBytes()); buffer.rewind();/*from www . j ava2 s . c o m*/ staging.writeTempWriteBytes(buffer); Assert.assertTrue(staging.hasTempWriteBytes()); buffer.clear(); staging.readTempWriteBytes(buffer); Assert.assertEquals("Hello World", new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length))); staging.resetTempWriteBytes(); Assert.assertFalse(staging.hasTempWriteBytes()); }
From source file:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java
private void sockItDown(ContentDecoder decoder) throws IOException { tlog().debug("StreamingNHttpEntity: sockItDown()"); ByteBuffer buffer; int cnt;//from ww w . ja va 2 s. c om buffer = _alloctor.allocate(4096); do { buffer.clear(); if ((cnt = decoder.read(buffer)) == -1) break; if (cnt == 0) { if (buffer.hasRemaining()) break; else continue; } // buffer.flip(); byte[] bits = new byte[4096]; int len; while (buffer.hasRemaining()) { len = Math.min(4096, buffer.remaining()); buffer.get(bits, 0, len); storeBytes(bits, len); } } while (true); }
From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java
@Test public void test_tempRead() { ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("Hello World".getBytes()); buffer.flip();/*from ww w . j a va2 s . co m*/ staging.writeTempReadBytes(buffer); Assert.assertTrue(staging.hasTempReadBytes()); buffer.clear(); staging.readTempReadBytes(buffer); Assert.assertEquals("Hello World", new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length))); staging.resetTempReadBytes(); Assert.assertFalse(staging.hasTempReadBytes()); }