List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:com.sastix.cms.server.services.cache.CacheFileUtilsServiceImpl.java
@Override public byte[] downloadResource(URL url) throws IOException { //create buffer with capacity in bytes ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from ww w .ja v a 2 s. c om*/ ByteBuffer bufIn = ByteBuffer.allocate(1024); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); int bytesRead; while ((bytesRead = rbc.read(bufIn)) > 0) { baos.write(bufIn.array(), 0, bytesRead); bufIn.rewind(); } bufIn.clear(); return baos.toByteArray(); } finally { baos.close(); } }
From source file:Main.java
public static byte[] genSirfCommand(String commandHexa) { int length = commandHexa.length() / 2; ByteBuffer command = ByteBuffer.allocate(length); command.put(new BigInteger(commandHexa, 16).toByteArray(), 1, length); return command.array(); }
From source file:ConversionUtil.java
public static byte[] convertToByteArray(char value) { byte[] bytes = new byte[2]; ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.putChar(value);//from ww w . jav a 2 s .co m return buffer.array(); // buffer.get(bytes); /* for (int i =0;i<bytes.length; ++i) { int offset = (bytes.length -i-1) *8; bytes[i] = (byte)((value & (0xff << offset)) >>> offset); } **/ // return bytes; }
From source file:SelfClassLoader.java
private byte[] loadClassBytes(String className) throws ClassNotFoundException { try {//from ww w .ja v a2 s . c o m String classFile = getClassFile(className); FileInputStream fis = new FileInputStream(classFile); FileChannel fileC = fis.getChannel(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WritableByteChannel outC = Channels.newChannel(baos); ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { int i = fileC.read(buffer); if (i == 0 || i == -1) { break; } buffer.flip(); outC.write(buffer); buffer.clear(); } fis.close(); return baos.toByteArray(); } catch (IOException fnfe) { throw new ClassNotFoundException(className); } }
From source file:com.example.IdGenerator.java
public long generateLong() { final LocalDateTime now = LocalDateTime.now(clock); final Duration duration = Duration.between(BASE, now); final int high = (int) duration.getSeconds(); final int low = duration.getNano(); final byte[] hbs = ByteBuffer.allocate(4).putInt(high).array(); final byte[] lbs = ByteBuffer.allocate(4).putInt(low).array(); final byte[] bytes = new byte[8]; System.arraycopy(hbs, 0, bytes, 0, 4); System.arraycopy(lbs, 0, bytes, 4, 4); final ByteBuffer buffer = ByteBuffer.allocate(8).put(bytes, 0, 8); buffer.flip();// ww w. j av a 2s . c om return buffer.getLong(); }
From source file:com.mozilla.bagheera.util.IdUtil.java
/** * Takes a given id and prefixes it with a byte character and the date in a non-random fashion * This method expects id to be something like a UUID consisting only of hex characters. If id * is something else this will produce unpredictable results. * @param id/* www . j a va 2 s . c o m*/ * @param d * @return * @throws IOException */ public static byte[] nonRandByteBucketizeId(String id, Date d) throws IOException { if (StringUtils.isBlank(id)) { throw new IllegalArgumentException("id cannot be null or empty"); } if (d == null) { throw new IllegalArgumentException("date cannot be null"); } // bucket byte + SDF bytes + id bytes ByteBuffer buf = ByteBuffer.allocate(9 + id.length()); int bucket = 0; if (id.length() >= 2) { // Munge two hex characters into the range of a single byte bucket = Integer.parseInt(id.substring(0, 2), 16) - 128; } else { bucket = Integer.parseInt(id, 16) - 128; } buf.put((byte) bucket); buf.put(SDF.format(d).getBytes()); buf.put(id.getBytes()); return buf.array(); }
From source file:eu.pursuit.core.ItemName.java
public byte[] toByteArray() { int length = 0; if (scopeId != null) { length += scopeId.getLength();// ww w . j a v a 2 s . com } if (rendezvousId != null) { length += rendezvousId.getId().length; } ByteBuffer buffer = ByteBuffer.allocate(length); if (scopeId != null) { scopeId.fill(buffer); } if (rendezvousId != null) { buffer.put(rendezvousId.getId()); } return buffer.array(); }
From source file:net.beaconpe.jraklib.protocol.AcknowledgePacket.java
@Override protected void _encode() { ByteBuffer payload = ByteBuffer.allocate(1024); int count = packets.length; int records = 0; if (count > 0) { int pointer = 0; int start = packets[0]; int last = packets[0]; while (pointer + 1 < count) { int current = packets[pointer++]; int diff = current - last; if (diff == 1) { last = current;// w w w.ja v a 2 s. c om } else if (diff > 1) { //Forget about duplicated packets (bad queues?) if (start == last) { payload.put((byte) 0x01); payload.put(Binary.writeLTriad(start)); start = last = current; } else { payload.put((byte) 0x00); payload.put(Binary.writeLTriad(start)); payload.put(Binary.writeLTriad(last)); start = last = current; } records = records + 1; } } if (start == last) { payload.put((byte) 0x01); payload.put(Binary.writeLTriad(start)); } else { payload.put((byte) 0x00); payload.put(Binary.writeLTriad(start)); payload.put(Binary.writeLTriad(last)); } records = records + 1; } putShort((short) records); put(ArrayUtils.subarray(payload.array(), 0, payload.position())); }
From source file:RegexProperties.java
public void load(FileInputStream inStream) throws IOException, PatternSyntaxException { FileChannel fc = inStream.getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fc.size()); fc.read(bb);/* ww w.j a v a2 s. c om*/ bb.flip(); String fileContent = new String(bb.array()); Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE); Matcher matcher = pattern.matcher(fileContent); while (matcher.find()) { String line = matcher.group(1); if (line != null && !"".equals(line.trim()) && !line.startsWith("#") && !line.startsWith("!")) { String keyValue[] = null; if (line.indexOf("=") > 0) keyValue = line.split("=", 2); else keyValue = line.split(":", 2); if (keyValue != null) { super.put(keyValue[0].trim(), keyValue[1]); } } } fc = null; bb = null; }
From source file:io.opencensus.contrib.spring.sleuth.v1x.OpenCensusSleuthSpan.java
@SuppressWarnings("deprecation") private static SpanContext fromSleuthSpan(org.springframework.cloud.sleuth.Span span) { return SpanContext.create( TraceId.fromBytes(ByteBuffer.allocate(TraceId.SIZE).putLong(span.getTraceIdHigh()) .putLong(span.getTraceId()).array()), SpanId.fromBytes(ByteBuffer.allocate(SpanId.SIZE).putLong(span.getSpanId()).array()), Boolean.TRUE.equals(span.isExportable()) ? sampledOptions : notSampledOptions); }