List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:ValidateLicenseHeaders.java
/** * Add the default jboss lgpl header// w w w . j a v a 2 s .c o m */ static void addDefaultHeader(File javaFile) throws IOException { if (addDefaultHeader) { FileInputStream fis = new FileInputStream(javaFile); FileChannel fc = fis.getChannel(); int size = (int) fc.size(); ByteBuffer contents = ByteBuffer.allocate(size); fc.read(contents); fis.close(); ByteBuffer hdr = ByteBuffer.wrap(DEFAULT_HEADER.getBytes()); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(hdr.array()); fos.write(contents.array()); fos.close(); } noheaders.add(javaFile); }
From source file:jBittorrentAPI.utils.Utils.java
/** * Concatenate the 2 byte arrays/* w ww. ja v a2s. c o m*/ * @param a byte[] * @param b byte[] * @return byte[] */ public static byte[] concat2(byte[] a, byte[] b) { ByteBuffer bb = ByteBuffer.allocate(a.length + b.length); bb.put(a); bb.put(b); return bb.array(); }
From source file:org.apache.solr.handler.TestBlobHandler.java
public static void postData(CloudSolrClient cloudClient, String baseUrl, String blobName, ByteBuffer bytarr) throws IOException { HttpPost httpPost = null;/* ww w .j a v a2 s . c om*/ HttpEntity entity; String response = null; try { httpPost = new HttpPost(baseUrl + "/.system/blob/" + blobName); httpPost.setHeader("Content-Type", "application/octet-stream"); httpPost.setEntity(new ByteArrayEntity(bytarr.array(), bytarr.arrayOffset(), bytarr.limit())); entity = cloudClient.getLbClient().getHttpClient().execute(httpPost).getEntity(); try { response = EntityUtils.toString(entity, StandardCharsets.UTF_8); Map m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response))); assertFalse("Error in posting blob " + getAsString(m), m.containsKey("error")); } catch (JSONParser.ParseException e) { log.error("$ERROR$", response, e); fail(); } } finally { httpPost.releaseConnection(); } }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java
public static byte[] getBytesFromByteBuffer(ByteBuffer buffer) { // Be careful *NOT* to perform anything that will modify the buffer's position or limit byte[] bytes = new byte[buffer.limit() - buffer.position()]; if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.position(), bytes, 0, bytes.length); } else {/*w w w .j a va 2 s.c o m*/ buffer.duplicate().get(bytes, buffer.position(), bytes.length); } return bytes; }
From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java
private static JsonNode readFromBuffer(JsonRpcProtocol protocol, ByteBuffer data) throws IOException { byte[] array; int arrayOffset; int arrayLength; int oldPos = data.position(); if (data.hasArray()) { array = data.array(); arrayOffset = data.arrayOffset(); arrayLength = data.remaining();/*from ww w . j ava2 s. com*/ } else { array = new byte[data.remaining()]; data.get(array); arrayOffset = 0; arrayLength = array.length; } data.position(oldPos); JsonNode node; try (JsonParser parser = protocol.getObjectReader().getFactory().createParser(array, arrayOffset, arrayLength)) { node = parser.readValueAsTree(); } return node; }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static ByteBuffer clone(ByteBuffer o) { assert o != null; if (o.remaining() == 0) return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY); ByteBuffer clone = ByteBuffer.allocate(o.remaining()); if (o.isDirect()) { for (int i = o.position(); i < o.limit(); i++) { clone.put(o.get(i));/*from www. jav a2s . co m*/ } clone.flip(); } else { System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining()); } return clone; }
From source file:com.smartitengineering.cms.api.impl.Utils.java
public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException { int allocationBlockSize = 2000; int capacity = allocationBlockSize; int length = 0; ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize); boolean notEof = true; while (notEof) { try {/*from w w w . j a va 2 s. c o m*/ buffer.put(in.readByte()); if (++length >= capacity) { capacity += allocationBlockSize; buffer.limit(capacity); } } catch (EOFException ex) { notEof = false; } } String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length)); return string; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static String bytesToHex(ByteBuffer bytes) { StringBuilder sb = new StringBuilder(); for (int i = bytes.position() + bytes.arrayOffset(); i < bytes.limit() + bytes.arrayOffset(); i++) { int bint = bytes.array()[i] & 0xff; if (bint <= 0xF) // toHexString does not 0 pad its results. sb.append("0"); sb.append(Integer.toHexString(bint)); }//from ww w . j av a2 s . c o m return sb.toString(); }
From source file:jp.andeb.obbutil.ObbUtilMain.java
private static boolean doAdd(String[] args) { final CommandLine commandLine; try {//from www.j a v a 2s .c o m final CommandLineParser parser = new GnuParser(); commandLine = parser.parse(OPTIONS_FOR_ADD, args); } catch (MissingArgumentException e) { System.err.println("??????: " + e.getOption().getOpt()); printUsage(PROGNAME); return false; } catch (MissingOptionException e) { System.err.println("??????: " + e.getMissingOptions()); printUsage(PROGNAME); return false; } catch (UnrecognizedOptionException e) { System.err.println("????: " + e.getOption()); printUsage(PROGNAME); return false; } catch (ParseException e) { System.err.println(e.getMessage()); printUsage(PROGNAME); return false; } final String pkgName = commandLine.getOptionValue(PACKAGE_NAME.getOpt()); final String versionStr = commandLine.getOptionValue(OBB_VERSION.getOpt()); final Integer version = toInteger(versionStr); if (version == null) { System.err.println("??????: " + versionStr); printUsage(PROGNAME); return false; } final boolean isOverlay = commandLine.hasOption(OVERLAY_FLAG.getOpt()); final String saltStr = commandLine.getOptionValue(SALT.getOpt()); final byte[] salt; if (saltStr == null) { salt = null; } else { salt = toByteArray(saltStr, ObbInfoV1.SALT_LENGTH); if (salt == null) { System.err.println("????: " + saltStr); printUsage(PROGNAME); return false; } } final String[] nonRecognizedArgs = commandLine.getArgs(); if (nonRecognizedArgs.length == 0) { System.err.println("????????"); printUsage(PROGNAME); return false; } if (nonRecognizedArgs.length != 1) { System.err.println("???????"); printUsage(PROGNAME); return false; } final File targetFile = new File(nonRecognizedArgs[0]); final RandomAccessFile targetRaFile; try { targetRaFile = new RandomAccessFile(targetFile, "rw"); } catch (FileNotFoundException e) { System.err.println("????: " + targetFile.getPath()); return false; } try { try { final ObbInfoV1 info = ObbInfoV1.fromFile(targetRaFile); System.err.println( "?? OBB ???????: " + info.toString()); return false; } catch (IOException e) { System.err .println("????????: " + targetFile.getPath()); return false; } catch (NotObbException e) { // } int flag = 0; if (isOverlay) { flag |= ObbInfoV1.FLAG_OVERLAY; } if (salt != null) { flag |= ObbInfoV1.FLAG_SALTED; } final ObbInfoV1 obbInfo = new ObbInfoV1(flag, salt, pkgName, version.intValue()); final ByteBuffer obbInfoBytes = obbInfo.toBytes(); // ??? targetRaFile.setLength(targetRaFile.length() + obbInfoBytes.remaining()); targetRaFile.seek(targetRaFile.length() - obbInfoBytes.remaining()); targetRaFile.write(obbInfoBytes.array(), obbInfoBytes.arrayOffset(), obbInfoBytes.remaining()); } catch (IOException e) { System.err.println("OBB ?????????: " + targetFile.getPath()); return false; } finally { try { targetRaFile.close(); } catch (IOException e) { System.err.println("OBB ?????????: " + targetFile.getPath()); return false; } } System.err.println("OBB ??????????: " + targetFile.getPath()); return true; }
From source file:io.blobkeeper.file.util.FileUtils.java
public static boolean isFileEmpty(@NotNull File file, @NotNull IndexElt elt) { ByteBuffer fourBytes = ByteBuffer.allocate(4); try {//from w w w. j a va 2s .co m file.getFileChannel().read(fourBytes, elt.getOffset()); } catch (IOException e) { log.error("Can't read blob file", e); throw new IllegalArgumentException(e); } fourBytes.flip(); for (byte b : fourBytes.array()) { if (b != 0) { return false; } } return true; }