List of usage examples for java.nio ByteBuffer get
public ByteBuffer get(byte[] dest, int off, int len)
From source file:Main.java
private static byte[] byteBufferToArray(ByteBuffer bb) { byte[] bytes = new byte[bb.position()]; bb.rewind();/* w ww .j a v a 2 s . c o m*/ bb.get(bytes, 0, bytes.length); return bytes; }
From source file:com.ntsync.shared.ContactGroup.java
private static String readRawString(ByteBuffer src) throws UnsupportedEncodingException { int len = src != null ? src.remaining() : -1; if (len >= 0) { byte[] output = new byte[len]; src.get(output, 0, len); // Android has UTF-8 as default return new String(output, SyncDataHelper.DEFAULT_CHARSET_NAME); }//from ww w . j a va 2s. co m return null; }
From source file:com.github.chenxiaolong.dualbootpatcher.RomUtils.java
private static boolean usesLiveWallpaper(RomInformation info, MbtoolInterface iface) throws IOException, MbtoolException, MbtoolCommandException { String wallpaperInfoPath = info.getDataPath() + "/system/users/0/wallpaper_info.xml"; int id = -1;/*from ww w.j ava2 s . co m*/ try { id = iface.fileOpen(wallpaperInfoPath, new short[] { FileOpenFlag.RDONLY }, 0); StatBuf sb = iface.fileStat(id); // Check file size if (sb.st_size < 0 || sb.st_size > 1024) { return false; } // Read file into memory byte[] data = new byte[(int) sb.st_size]; int nWritten = 0; while (nWritten < data.length) { ByteBuffer newData = iface.fileRead(id, 10240); int nRead = newData.limit() - newData.position(); newData.get(data, nWritten, nRead); nWritten += nRead; } iface.fileClose(id); id = -1; String xml = new String(data, Charsets.UTF_8); return xml.contains("component="); } finally { if (id >= 0) { try { iface.fileClose(id); } catch (IOException e) { // Ignore } } } }
From source file:com.github.chenxiaolong.dualbootpatcher.RomUtils.java
public static CacheWallpaperResult cacheWallpaper(Context context, RomInformation info, MbtoolInterface iface) throws IOException, MbtoolException, MbtoolCommandException { if (usesLiveWallpaper(info, iface)) { // We can't render a snapshot of a live wallpaper return CacheWallpaperResult.USES_LIVE_WALLPAPER; }/* w w w. j a va 2s . co m*/ String wallpaperPath = info.getDataPath() + "/system/users/0/wallpaper"; File wallpaperCacheFile = new File(info.getWallpaperPath()); FileOutputStream fos = null; int id = -1; try { id = iface.fileOpen(wallpaperPath, new short[] {}, 0); // Check if we need to re-cache the file StatBuf sb = iface.fileStat(id); if (wallpaperCacheFile.exists() && wallpaperCacheFile.lastModified() / 1000 > sb.st_mtime) { Log.d(TAG, "Wallpaper for " + info.getId() + " has not been changed"); return CacheWallpaperResult.UP_TO_DATE; } // Ignore large wallpapers if (sb.st_size < 0 || sb.st_size > 20 * 1024 * 1024) { return CacheWallpaperResult.FAILED; } // Read file into memory byte[] data = new byte[(int) sb.st_size]; int nWritten = 0; while (nWritten < data.length) { ByteBuffer newData = iface.fileRead(id, 10240); int nRead = newData.limit() - newData.position(); newData.get(data, nWritten, nRead); nWritten += nRead; } iface.fileClose(id); id = -1; fos = new FileOutputStream(wallpaperCacheFile); // Compression can be very slow (more than 10 seconds) for a large wallpaper, so we'll // just cache the actual file instead fos.write(data); // Load into bitmap //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //if (bitmap == null) { // return false; //} //bitmap.compress(Bitmap.CompressFormat.WEBP, 100, fos); //bitmap.recycle(); // Invalidate picasso cache Picasso.with(context).invalidate(wallpaperCacheFile); Log.d(TAG, "Wallpaper for " + info.getId() + " has been cached"); return CacheWallpaperResult.UPDATED; } finally { if (id >= 0) { try { iface.fileClose(id); } catch (IOException e) { // Ignore } } IOUtils.closeQuietly(fos); } }
From source file:gobblin.util.io.StreamUtils.java
/** * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is * consumed by this operation; eg in.remaining() will be 0 after it completes successfully. * @param in ByteBuffer to write into the OutputStream * @param out Destination stream//from ww w .j a va 2s. c om * @throws IOException If there is an error writing into the OutputStream */ public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException { final int BUF_SIZE = 8192; if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + in.position(), in.remaining()); } else { final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)]; while (in.remaining() > 0) { int bytesToRead = Math.min(in.remaining(), BUF_SIZE); in.get(b, 0, bytesToRead); out.write(b, 0, bytesToRead); } } }
From source file:backtype.storm.utils.Utils.java
public static byte[] toByteArray(ByteBuffer buffer) { byte[] ret = new byte[buffer.remaining()]; buffer.get(ret, 0, ret.length); return ret;/*from w ww . j a v a 2 s.c o m*/ }
From source file:com.fujitsu.dc.test.jersey.bar.BarInstallTestUtils.java
/** * bar?.// w w w. j a v a 2s.c o m * @param barFile bar? * @return ??bar */ public static byte[] readBarFile(File barFile) { InputStream is = ClassLoader.getSystemResourceAsStream(barFile.getPath()); ByteBuffer buff = ByteBuffer.allocate(READ_BUFFER_SIZE); log.debug(String.valueOf(buff.capacity())); try { byte[] bbuf = new byte[SIZE_KB]; int size; while ((size = is.read(bbuf)) != -1) { buff.put(bbuf, 0, size); } } catch (IOException e) { throw new RuntimeException("failed to load bar file:" + barFile.getPath(), e); } finally { try { is.close(); } catch (IOException e) { throw new RuntimeException("failed to close bar file:" + barFile.getPath(), e); } } int size = buff.position(); buff.flip(); byte[] retv = new byte[size]; buff.get(retv, 0, size); return retv; }
From source file:adminpassword.Decryption.java
@SuppressWarnings("static-access") public String decrypt(String encryptedText, String idKey) throws Exception { String password = idKey;//from w w w . java2 s. co m Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //strip off the salt and iv ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText)); byte[] saltBytes = new byte[20]; buffer.get(saltBytes, 0, saltBytes.length); byte[] ivBytes1 = new byte[cipher.getBlockSize()]; buffer.get(ivBytes1, 0, ivBytes1.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length]; buffer.get(encryptedTextBytes); // Deriving the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); }
From source file:net.darkmist.alib.io.BufferUtil.java
public static byte[] asBytes(ByteBuffer buf) { buf = buf.duplicate();/* w ww .j av a2s.co m*/ /* To use buf.array() the buffer must: * be writable as the array will be writable * have arrayOffset() == 0 or the array will not start at the right location * the returned array must be the same length as the buffer's limit or it will be the wrong size. */ if (!buf.isReadOnly() && buf.hasArray() && buf.arrayOffset() == 0) { logger.debug("!read-only, hasArray && offset is 0"); byte[] ret = buf.array(); if (ret.length == buf.limit()) return ret; logger.debug("length of array !=limit, doing copy..."); } byte[] bytes = new byte[buf.limit()]; buf.get(bytes, 0, buf.limit()); return bytes; }
From source file:com.ntsync.shared.RawContact.java
private static String readRawString(ByteBuffer src) throws UnsupportedEncodingException { if (src != null) { int len = src.remaining(); if (len >= 0) { byte[] output = new byte[len]; src.get(output, 0, len); // Android has UTF-8 as default return new String(output, SyncDataHelper.DEFAULT_CHARSET_NAME); }/*from www. j a v a2s . c om*/ } return null; }