List of usage examples for java.nio ByteBuffer wrap
public static ByteBuffer wrap(byte[] array)
From source file:com.delphix.session.service.ServiceUUID.java
@Override public byte[] getBytes() { ByteBuffer buffer = ByteBuffer.wrap(new byte[16]); buffer.putLong(uuid.getMostSignificantBits()); buffer.putLong(uuid.getLeastSignificantBits()); return buffer.array(); }
From source file:com.juniform.JUniformPackerJSON.java
@Override public JUniformObject toUniformObjectFromBytes(byte[] bytes) { ByteBuffer buffer = ByteBuffer.wrap(bytes); return this.toUniformObjectFromString(Charset.defaultCharset().decode(buffer).toString()); }
From source file:com.github.kubakaszycki.jkhttpd.api.FileURLHandler.java
@Override public void handle(String res, HttpRequest req, HttpResponse resp) throws IOException, IllegalArgumentException { File relativeFile = new File(baseDir, res); if (!relativeFile.exists()) { resp.setCode(404);/*from ww w . ja va 2s .co m*/ resp.setCodeString(); return; } if (relativeFile.isDirectory()) { resp.setContents(CharBuffer.wrap(DirectoryListing.doDirectoryListing(relativeFile, res))); } else { resp.setContents( Charset.defaultCharset().decode(ByteBuffer.wrap(FileUtils.readFileToByteArray(relativeFile)))); } resp.setCode(200); resp.setCodeString(); }
From source file:com.spotify.hdfs2cass.CassandraPartitioner.java
@Override public int getPartition(Text key, Text value, int numReducers) { final int partition; final BigIntegerToken token = partitioner.getToken(ByteBuffer.wrap(key.getBytes())); final int index = Collections.binarySearch(tokenNodes, new TokenNode(token), SEARCH_COMPARATOR); if (index >= 0) { final int multiple = numReducers / tokenNodes.size(); partition = index + (multiple * RANDOM.nextInt(multiple)); } else {/*from w w w. j av a 2 s .c o m*/ throw new RuntimeException("Failed to find a node for token " + token); } return partition; }
From source file:biz.karms.sinkit.ejb.util.CIDRUtils.java
public static ImmutablePair<String, String> getStartEndAddresses(final String cidr) throws UnknownHostException { //TODO: This is silly. Refactor CIDRUtils so as to accept actual IPs as well as subnets. //TODO: Validate the thing before processing. Guava? final String fixedCIDR; if (!cidr.contains("/")) { //IPv6? Hmmm... if (cidr.contains(":")) { fixedCIDR = cidr + "/128"; } else {/* ww w. j a v a2 s .c o m*/ fixedCIDR = cidr + "/32"; } } else { fixedCIDR = cidr; } final int index = fixedCIDR.indexOf("/"); final InetAddress inetAddress = InetAddress.getByName(fixedCIDR.substring(0, index)); final int prefixLength = Integer.parseInt(fixedCIDR.substring(index + 1)); final ByteBuffer maskBuffer; if (inetAddress.getAddress().length == 4) { maskBuffer = ByteBuffer.allocate(4).putInt(-1); } else { maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L); } final BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength); final ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress()); final BigInteger ipVal = new BigInteger(1, buffer.array()); final BigInteger startIp = ipVal.and(mask); final BigInteger endIp = startIp.add(mask.not()); return new ImmutablePair<>(String.format("%040d", startIp), String.format("%040d", endIp)); }
From source file:cfa.vo.interop.EncodeDoubleArray.java
public static String encodeBase64(double[] data, boolean swapByteOrder) throws IOException { byte[] decodedData = doubleToByte(data); if (swapByteOrder) { ByteBuffer buf = ByteBuffer.wrap(decodedData); buf = buf.order(ByteOrder.LITTLE_ENDIAN); buf.get(decodedData);//from www . j a v a 2s .c o m } Base64 codec = new Base64(); byte[] encodedData = codec.encode(decodedData); String result = new String(encodedData); return result; }
From source file:io.github.dsheirer.sample.adapter.ChannelShortAdapter.java
@Override public float[] convert(byte[] samples) { float[] processed = new float[samples.length / 4]; int pointer = 0; /* Wrap byte array in a byte buffer so we can process them as shorts */ ByteBuffer buffer = ByteBuffer.wrap(samples); /* Set endian to correct byte ordering */ buffer.order(mByteOrder);//from ww w . j ava 2 s . co m while (buffer.hasRemaining()) { if (mMixerChannel == MixerChannel.LEFT) { processed[pointer] = mMap.get(buffer.getShort()); /* Throw away the right channel */ buffer.getShort(); } else { /* Throw away the left channel */ buffer.getShort(); processed[pointer] = mMap.get(buffer.getShort()); } pointer++; } return processed; }
From source file:com.woodcomputing.bobbin.format.PESFormat.java
@Override public Design load(File file) { byte[] bytes = null; try (InputStream is = new FileInputStream(file)) { bytes = IOUtils.toByteArray(is); } catch (IOException ex) { log.catching(ex);//from w w w .ja v a2s . com } ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); Design design = new Design(); log.debug("Magic: {}{}{}{}", (char) bb.get(), (char) bb.get(), (char) bb.get(), (char) bb.get()); int pecStart = bb.getInt(8); log.debug("PEC Start: {}", pecStart); byte colorCount = bb.get(pecStart + 48); log.debug("Color Count: {}", colorCount); int colors[] = new int[colorCount]; for (int i = 0; i < colorCount; i++) { colors[i] = bb.get() & 0xFF; log.debug("Color[{}] = {}", i, colors[i]); } bb.position(pecStart + 532); int x; int y; int colorChanges = 0; PESColor color = pesColorMap.get(colors[colorChanges++]); StitchGroup stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); while (true) { x = bb.get() & 0xFF; y = bb.get() & 0xFF; if (x == 0xFF && y == 0x00) { log.debug("End of stitches"); break; } if (x == 0xFE && y == 0xB0) { int colorIndex = bb.get() & 0xFF; log.debug("Color change: {}", colorIndex); color = pesColorMap.get(colors[colorChanges++]); stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); continue; } if ((x & 0x80) > 0) { log.debug("Testing X: {} - X & 0x80: {}", x, x & 0x80); if ((x & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((x & 0x10) > 0) { log.debug("Stich type JUMP"); } x = ((x & 0x0F) << 8) + y; if ((x & 0x800) > 0) { x -= 0x1000; } y = bb.get() & 0xFF; } else if (x >= 0x40) { x -= 0x80; } if ((y & 0x80) > 0) { log.debug("Testing Y: {} - Y & 0x80: {}", y, y & 0x80); if ((y & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((y & 0x10) > 0) { log.debug("Stich type JUMP"); } y = ((y & 0x0F) << 8) + bb.get() & 0xFF; if ((y & 0x800) > 0) { y -= 0x1000; } } else if (y >= 0x40) { y -= 0x80; } // Stitch designStitch = new Stitch(cx, -cy, nx, -ny); // stitchGroup.getStitches().add(designStitch); log.debug("X: {} Y: {}", x, y); } log.debug("Color Changes: {}", colorChanges); return design; }
From source file:com.willetinc.hadoop.mapreduce.dynamodb.AttributeValueIOUtilsTest.java
@Test public void testToStringB() { final byte[] BYTES = new byte[] { 0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF }; final String ENCODED_VALUE = Base64.encodeBase64String(BYTES); ByteBuffer buf = ByteBuffer.wrap(BYTES); AttributeValue attr = new AttributeValue().withB(buf); String result = AttributeValueIOUtils.toString(Types.BINARY, attr); assertEquals(ENCODED_VALUE, result); }
From source file:me.footlights.core.crypto.SecretKeyTest.java
/** * Test encryption and decryption using test vectors from * @url http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip. *//* www .j a va 2 s .c om*/ @Test public void testEncryptDecrypt() throws Throwable { for (String[] v : TEST_VECTORS) { int i = 0; String algorithm = v[i++]; String mode = v[i++]; byte[] secret = Hex.decodeHex(v[i++].toCharArray()); i++; // We don't actually use the IV. byte[] plaintext = Hex.decodeHex(v[i++].toCharArray()); ByteBuffer plainbuf = ByteBuffer.wrap(Arrays.copyOf(plaintext, plaintext.length)); byte[] ciphertext = Hex.decodeHex(v[i++].toCharArray()); ByteBuffer cipherbuf = ByteBuffer.wrap(Arrays.copyOf(ciphertext, ciphertext.length)); SecretKey key = SecretKey.newGenerator().setAlgorithm(algorithm).setBytes(secret).generate(); CipherBuilder builder = key.newCipherBuilder().setMode(mode); try { Cipher e = builder.setOperation(Operation.ENCRYPT).build(); Cipher d = builder.setOperation(Operation.DECRYPT).build(); assertArrayEquals(ciphertext, e.doFinal(plaintext)); assertArrayEquals(plaintext, d.doFinal(ciphertext)); // Do it again, but this time with ByteBuffers. e.doFinal(plainbuf, cipherbuf); assertArrayEquals(ciphertext, cipherbuf.array()); d.doFinal(cipherbuf, plainbuf); assertArrayEquals(plaintext, plainbuf.array()); } catch (InvalidKeyException e) { fail("Unable to construct an AES cipher with a " + (8 * secret.length) + "-bit key; is the JCE unlimited strength policy installed?"); } } }