List of usage examples for java.nio ByteBuffer array
public final byte[] array()
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 w ww . j av a2 s . c o m 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.mrstampy.gameboot.otp.websocket.OtpEncryptedWebSocketHandler.java
private byte[] extractArray(WebSocketSession session, BinaryMessage message) throws IOException { ByteBuffer buf = message.getPayload(); if (buf.hasArray()) return buf.array(); int size = buf.remaining(); if (size == 0) { log.error("No message, closing session {}", session); session.close();//w ww . j a v a 2 s .c om return null; } byte[] b = new byte[size]; buf.get(b, 0, b.length); return b; }
From source file:experts.net.ip6.ULUA.java
/** * Generate Global ID according to RFC 4193 Section 3.2.2. * * @param timeStamp/*from w w w.j av a2 s. c o m*/ * 64-bit NTP format */ public final void generateGlobalID(long timeStamp) { ByteBuffer buf = ByteBuffer.allocate(16); buf.putLong(timeStamp); interfaceID.forEach(buf::putShort); byte[] digest = DigestUtils.sha1(buf.array()); buf = ByteBuffer.allocate(6); buf.put(GLOBAL_ID_PREFIX).put(digest, 15, 5); globalID = toList(buf); }
From source file:byps.test.TestSerializeInlineInstances.java
private void internalTestSerializeInlineInstance(Actor obj, String jsonText) throws BException { BOutput bout = transport.getOutput(); bout.header.messageId = 123;//w w w . j ava 2s. com bout.header.targetId = new BTargetId(1, 1, 2); bout.store(obj); ByteBuffer buf = bout.toByteBuffer(); TestUtils.printBuffer(log, buf); if (TestUtils.protocol == BProtocolJson.BINARY_MODEL && jsonText != null) { try { String jsonTextR = new String(buf.array(), buf.position(), buf.limit(), "UTF-8"); TestUtils.assertEquals(log, "jsonText", jsonText, jsonTextR); } catch (UnsupportedEncodingException ignored) { } } BInput bin = transport.getInput(null, buf); Object objR = (Object) bin.load(); TestUtils.assertEquals(log, "obj.class", obj.getClass(), objR.getClass()); if (obj.position == null) { obj.position = new Matrix2D(); } TestUtils.assertEquals(log, "obj", obj, objR); }
From source file:com.github.sebhoss.identifier.service.SuppliedIdentifiers.java
private String convertUuidToBase64(final UUID uuid) { final ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); return removePadding(encoder.encodeToString(bb.array())); }
From source file:com.github.c77.base_driver.kobuki.KobukiPacketReader.java
public final void newPacket(ByteBuffer buff) { // look for main headers in buffer if (curState == parserState.INIT || curState == parserState.COMPLETE) { for (int i = 0; i < buff.array().length - 2; i++) { // currently ignoring -86|85 split case... should be rare if (buff.array()[i] == -86 && buff.array()[i + 1] == 85) { // Header 0 and Header 1 means packet curState = parserState.PARTIAL; if ((buff.array().length - i) >= (buff.array()[i + 2] + 4)) { // Whole packet is in the buffer. +3 accounts for headers and size. Don't think this is right. Change(+4 for checksum) byte[] packet = new byte[byteToInt(buff.array()[i + 2]) + 2]; // make byte array of proper size. +1 just in case!!!! Needs to be + 2 to account for checksum and length //If x + 2 is the length of the payload, the length of payload + length + check sum = length of payload + 2 System.arraycopy(buff.array(), i + 2, packet, 0, byteToInt(buff.array()[i + 2]) + 2); //copy in data needed if (verifyChecksum(byteToInt(buff.array()[i + 2]) + 2, packet)) { System.arraycopy(packet, 1, packet, 0, packet.length - 1); goodPacket(byteToInt(buff.array()[i + 2]), packet); // send goodPacket the array and length of array. At this point, the packet contains ONLY the payload... } else { //packet not valid }/* ww w. j ava 2s. co m*/ curState = parserState.COMPLETE; } else { System.arraycopy(buff.array(), i + 2, stored, 0, (buff.array().length - (i + 2))); //Copy the partial packet into the stored array (only for partial packets) bytesStillNeeded = (buff.array()[i + 2] + 2) - (buff.array().length - (i + 2)); totalSize = buff.array()[i + 2] + 2; //This is the payload length + 2. This accounts for both the length of the payload byte as well as the checksum byte bytesStoredIndex = buff.array().length - (i + 2); //Basically, in the case of a partial packet, this variable stores the length of the partial packet } } } } else { System.arraycopy(buff.array(), 0, stored, bytesStoredIndex, bytesStillNeeded); //Copy in the "rest" of the packet if (verifyChecksum(totalSize, stored)) { System.arraycopy(stored, 1, stored, 0, stored.length - 1); //Get the array without the checksum or length goodPacket(totalSize - 2, stored); } else { //packet not valid } curState = parserState.COMPLETE; } }
From source file:com.github.ambry.commons.BlobId.java
@Override public byte[] toBytes() { ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes()); idBuf.putShort(version);/* www. j ava 2 s .co m*/ idBuf.put(partitionId.getBytes()); idBuf.putInt(uuid.getBytes().length); idBuf.put(uuid.getBytes()); return idBuf.array(); }
From source file:com.sastix.cms.server.services.content.impl.ZipFileHandlerServiceImpl.java
@Override public DataMaps unzip(byte[] bytes) throws IOException { Map<String, String> foldersMap = new HashMap<>(); Map<String, byte[]> extractedBytesMap = new HashMap<>(); InputStream byteInputStream = new ByteArrayInputStream(bytes); //validate that it is a zip file if (isZipFile(bytes)) { try {/*w ww . j av a2s. co m*/ //get the zip file content ZipInputStream zis = new ZipInputStream(byteInputStream); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); if (!ze.isDirectory()) {//if entry is a directory, we should not add it as a file ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ByteBuffer bufIn = ByteBuffer.allocate(1024); int bytesRead; while ((bytesRead = zis.read(bufIn.array())) > 0) { baos.write(bufIn.array(), 0, bytesRead); bufIn.rewind(); } bufIn.clear(); extractedBytesMap.put(fileName, baos.toByteArray()); } finally { baos.close(); } } else { foldersMap.put(fileName, fileName); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { ex.printStackTrace(); } } DataMaps dataMaps = new DataMaps(); dataMaps.setBytesMap(extractedBytesMap); dataMaps.setFoldersMap(foldersMap); return dataMaps; }
From source file:nl.salp.warcraft4j.io.HttpDataReader.java
/** * {@inheritDoc}/* w ww . j ava2 s .c om*/ */ @Override protected int readData(ByteBuffer buffer) throws DataReadingException { try { int read = 0; byte[] dataArray; if (buffer.hasArray()) { read = responseStream.read(buffer.array()); } else { byte[] data = new byte[buffer.capacity()]; read = responseStream.read(data); buffer.put(data); } return read; } catch (IOException e) { throw new DataReadingException(e); } }
From source file:internal.diff.aws.service.AmazonS3ETagFileChecksumServiceImpl.java
@Override public String calculateChecksum(Path file) throws IOException { long fileSize = Files.size(file); int parts = (int) (fileSize / S3_MULTIPART_SIZE_LIMIT_IN_BYTES); parts += fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES > 0 ? 1 : 0; ByteBuffer checksumBuffer = ByteBuffer.allocate(parts * 16); SeekableByteChannel byteChannel = Files.newByteChannel(file); for (int part = 0; part < parts; part++) { int partSizeInBytes; if (part < parts - 1 || fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES == 0) { partSizeInBytes = S3_MULTIPART_SIZE_LIMIT_IN_BYTES; } else {//from w ww . j a v a 2 s . c om partSizeInBytes = (int) (fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES); } ByteBuffer partBuffer = ByteBuffer.allocate(partSizeInBytes); boolean endOfFile; do { endOfFile = byteChannel.read(partBuffer) == -1; } while (!endOfFile && partBuffer.hasRemaining()); checksumBuffer.put(DigestUtils.md5(partBuffer.array())); } if (parts > 1) { return DigestUtils.md5Hex(checksumBuffer.array()) + "-" + parts; } else { return Hex.encodeHexString(checksumBuffer.array()); } }