List of usage examples for java.nio ByteBuffer allocateDirect
public static ByteBuffer allocateDirect(int capacity)
From source file:haven.Utils.java
public static ByteBuffer mkbbuf(int n) { try {//from w w w . ja va 2 s . c o m return (ByteBuffer.allocateDirect(n).order(ByteOrder.nativeOrder())); } catch (OutOfMemoryError e) { /* At least Sun's class library doesn't try to collect * garbage if it's out of direct memory, which is pretty * stupid. So do it for it, then. */ System.gc(); return (ByteBuffer.allocateDirect(n).order(ByteOrder.nativeOrder())); } }
From source file:org.apache.hadoop.hive.llap.cache.BuddyAllocator.java
private ByteBuffer preallocateArenaBuffer(int arenaSize) { if (isMapped) { RandomAccessFile rwf = null; File rf = null;//from w w w . j a v a 2 s.co m Preconditions.checkArgument(isDirect, "All memory mapped allocations have to be direct buffers"); try { rf = File.createTempFile("arena-", ".cache", cacheDir.toFile()); rwf = new RandomAccessFile(rf, "rw"); rwf.setLength(arenaSize); // truncate (TODO: posix_fallocate?) // Use RW, not PRIVATE because the copy-on-write is irrelevant for a deleted file // see discussion in YARN-5551 for the memory accounting discussion ByteBuffer rwbuf = rwf.getChannel().map(MapMode.READ_WRITE, 0, arenaSize); return rwbuf; } catch (IOException ioe) { LlapIoImpl.LOG.warn("Failed trying to allocate memory mapped arena", ioe); // fail similarly when memory allocations fail throw new OutOfMemoryError("Failed trying to allocate memory mapped arena: " + ioe.getMessage()); } finally { // A mapping, once established, is not dependent upon the file channel that was used to // create it. delete file and hold onto the map IOUtils.closeQuietly(rwf); if (rf != null) { rf.delete(); } } } return isDirect ? ByteBuffer.allocateDirect(arenaSize) : ByteBuffer.allocate(arenaSize); }
From source file:org.lnicholls.galleon.togo.ToGo.java
public boolean Download(Video video, CancelDownload cancelDownload) { ServerConfiguration serverConfiguration = Server.getServer().getServerConfiguration(); GoBackConfiguration goBackConfiguration = Server.getServer().getGoBackConfiguration(); ArrayList videos = new ArrayList(); GetMethod get = null;//from w w w .j a va2 s.c o m try { URL url = new URL(video.getUrl()); Protocol protocol = new Protocol("https", new TiVoSSLProtocolSocketFactory(), 443); HttpClient client = new HttpClient(); // TODO How to get TiVo address?? client.getHostConfiguration().setHost(url.getHost(), 443, protocol); String password = Tools.decrypt(serverConfiguration.getMediaAccessKey()); if (video.getParentalControls() != null && video.getParentalControls().booleanValue()) { if (serverConfiguration.getPassword() == null) throw new NullPointerException("Parental Controls Password is null"); password = password + Tools.decrypt(serverConfiguration.getPassword()); } Credentials credentials = new UsernamePasswordCredentials("tivo", password); //client.getState().setCredentials("TiVo DVR", url.getHost(), credentials); client.getState().setCredentials(null, url.getHost(), credentials); get = new GetMethod(video.getUrl()); client.executeMethod(get); if (get.getStatusCode() != 200) { log.debug("Status code: " + get.getStatusCode()); return false; } InputStream input = get.getResponseBodyAsStream(); String path = serverConfiguration.getRecordingsPath(); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } String name = getFilename(video); File file = null; if (goBackConfiguration.isGroupByShow()) { if (video.getSeriesTitle() != null && video.getSeriesTitle().trim().length() > 0) { path = path + File.separator + clean(video.getSeriesTitle()); File filePath = new File(path); if (!filePath.exists()) filePath.mkdirs(); file = new File(path + File.separator + name); } else file = new File(path + File.separator + name); } else { file = new File(path + File.separator + name); } // TODO Handle retransfers /* if (file.exists() && video.getStatus()!=Video.STATUS_DOWNLOADING) { log.debug("duplicate file: "+file); try { List list = VideoManager.findByPath(file.getCanonicalPath()); if (list!=null && list.size()>0) { video.setDownloadSize(file.length()); video.setDownloadTime(0); video.setPath(file.getCanonicalPath()); video.setStatus(Video.STATUS_DELETED); VideoManager.updateVideo(video); return true; } } catch (HibernateException ex) { log.error("Video update failed", ex); } } */ log.info("Downloading: " + name); WritableByteChannel channel = new FileOutputStream(file, false).getChannel(); long total = 0; double diff = 0.0; ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 4); byte[] bytes = new byte[1024 * 4]; int amount = 0; int index = 0; long target = video.getSize(); long start = System.currentTimeMillis(); long last = start; while (amount == 0 && total < target) { while (amount >= 0 && !cancelDownload.cancel()) { if (index == amount) { amount = input.read(bytes); index = 0; total = total + amount; } while (index < amount && buf.hasRemaining()) { buf.put(bytes[index++]); } buf.flip(); int numWritten = channel.write(buf); if (buf.hasRemaining()) { buf.compact(); } else { buf.clear(); } if ((System.currentTimeMillis() - last > 10000) && (total > 0)) { try { video = VideoManager.retrieveVideo(video.getId()); if (video.getStatus() == Video.STATUS_DOWNLOADING) { diff = (System.currentTimeMillis() - start) / 1000.0; if (diff > 0) { video.setDownloadSize(total); video.setDownloadTime((int) diff); VideoManager.updateVideo(video); } } } catch (HibernateException ex) { log.error("Video update failed", ex); } last = System.currentTimeMillis(); } } if (cancelDownload.cancel()) { channel.close(); return false; } } diff = (System.currentTimeMillis() - start) / 1000.0; channel.close(); if (diff != 0) log.info("Download rate=" + (total / 1024) / diff + " KBps"); try { video.setPath(file.getCanonicalPath()); VideoManager.updateVideo(video); } catch (HibernateException ex) { log.error("Video update failed", ex); } } catch (MalformedURLException ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } catch (Exception ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } finally { if (get != null) get.releaseConnection(); } return true; }
From source file:com.android.camera.one.v2.OneCameraZslImpl.java
/** * Given an image reader, extracts the JPEG image bytes and then closes the * reader.//from w ww . j a v a 2s . co m * * @param img the image from which to extract jpeg bytes or compress to * jpeg. * @param degrees the angle to rotate the image clockwise, in degrees. Rotation is * only applied to YUV images. * @return The bytes of the JPEG image. Newly allocated. */ private byte[] acquireJpegBytes(Image img, int degrees) { ByteBuffer buffer; if (img.getFormat() == ImageFormat.JPEG) { Image.Plane plane0 = img.getPlanes()[0]; buffer = plane0.getBuffer(); byte[] imageBytes = new byte[buffer.remaining()]; buffer.get(imageBytes); buffer.rewind(); return imageBytes; } else if (img.getFormat() == ImageFormat.YUV_420_888) { buffer = mJpegByteBufferPool.acquire(); if (buffer == null) { buffer = ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * 3); } int numBytes = JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(img), buffer, JPEG_QUALITY, degrees); if (numBytes < 0) { throw new RuntimeException("Error compressing jpeg."); } buffer.limit(numBytes); byte[] imageBytes = new byte[buffer.remaining()]; buffer.get(imageBytes); buffer.clear(); mJpegByteBufferPool.release(buffer); return imageBytes; } else { throw new RuntimeException("Unsupported image format."); } }
From source file:com.serenegiant.media.TLMediaEncoder.java
/** * read raw bit stream from specific intermediate file * @param in/*from ww w. j a va 2 s .c om*/ * @param header * @param buffer * @param readBuffer * @throws IOException * @throws BufferOverflowException */ /*package*/static ByteBuffer readStream(final DataInputStream in, final TLMediaFrameHeader header, ByteBuffer buffer, final byte[] readBuffer) throws IOException { readHeader(in, header); if ((buffer == null) || header.size > buffer.capacity()) { buffer = ByteBuffer.allocateDirect(header.size); } buffer.clear(); final int max_bytes = Math.min(readBuffer.length, header.size); int read_bytes; for (int i = header.size; i > 0; i -= read_bytes) { read_bytes = in.read(readBuffer, 0, Math.min(i, max_bytes)); if (read_bytes <= 0) break; buffer.put(readBuffer, 0, read_bytes); } buffer.flip(); return buffer; }
From source file:com.castis.sysComp.PoisConverterSysComp.java
private void writeClientUIFile(List<sceneDTO> list, String platform, File file) throws FileNotFoundException { FileOutputStream fos = null;/*from ww w. ja v a 2s .com*/ String dir = filePolling.getValidFileDirectory(resultDir); String fileName = file.getName(); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { for (int i = 0; i < list.size(); i++) { sceneDTO scene = list.get(i); StringBuffer strBuffer = new StringBuffer(); if (i == 0) { strBuffer.append("policy"); strBuffer.append("|"); strBuffer.append(platform); strBuffer.append("|"); strBuffer.append("ClientUI"); strBuffer.append("\r\n"); } strBuffer.append("info"); strBuffer.append("|"); strBuffer.append(platform); strBuffer.append("|"); strBuffer.append(scene.getId()); strBuffer.append("|"); strBuffer.append(scene.getName()); strBuffer.append("|"); strBuffer.append(scene.getTemplateFileName()); strBuffer.append("|"); strBuffer.append(scene.getMenuId()); strBuffer.append("|"); strBuffer.append(scene.getMenuName()); strBuffer.append("|"); strBuffer.append(scene.getSpaceId()); strBuffer.append("|"); strBuffer.append(scene.getSpaceName()); strBuffer.append("|"); strBuffer.append(scene.getResolution()); strBuffer.append("|"); strBuffer.append(scene.getResolutionOnFocus()); strBuffer.append("|"); strBuffer.append(scene.getSizeLimit()); strBuffer.append("|"); strBuffer.append(scene.getClickable()); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } index = fileName.indexOf("_"); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } index = fileName.indexOf("."); if (index != -1) { fileName = fileName.substring(0, index) + ".csv"; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } }
From source file:com.castis.sysComp.PoisConverterSysComp.java
public void parseRegionFile(File file) throws Exception { String line = ""; FileInputStream in = null;//from w w w . j a va2s.co m Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String dir = filePolling.getValidFileDirectory(resultDir); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { in = new FileInputStream(file); isReader = new InputStreamReader(in, "UTF-16LE"); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { byte[] utf8 = line.getBytes("UTF-8"); String string = new String(utf8, "UTF-8"); String data[] = string.split("\t"); if (first == true) { first = false; if (data[0] == null || data[0].contains("region") == false) { throw new DataParsingException("data parsing error(not formatted)"); } continue; } if (data[0] == null || data[0].equals("")) { throw new DataParsingException("data parsing error(region id)"); } if (data[1] == null || data[1].equals("")) { throw new DataParsingException("data parsing error(region name)"); } if (data[2] == null || data[2].equals("")) { throw new DataParsingException("data parsing error(parent id)"); } StringBuffer strBuffer = new StringBuffer(); strBuffer.append(data[0]); strBuffer.append("\t"); strBuffer.append(data[1]); strBuffer.append("\t"); strBuffer.append(data[2]); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } }
From source file:org.apache.geode.internal.cache.Oplog.java
private static ByteBuffer allocateWriteBuf(OplogFile prevOlf) { if (prevOlf != null && prevOlf.writeBuf != null) { ByteBuffer result = prevOlf.writeBuf; prevOlf.writeBuf = null;//from w w w . j a v a 2 s . com return result; } else { return ByteBuffer.allocateDirect(Integer.getInteger("WRITE_BUF_SIZE", 32768)); } }
From source file:com.linkedin.databus.core.DbusEventBuffer.java
public static ByteBuffer allocateByteBuffer(int size, ByteOrder byteOrder, AllocationPolicy allocationPolicy, boolean restoreBuffers, File mmapSessionDir, File mmapFile) { ByteBuffer buffer = null;//from ww w . ja v a 2 s. co m switch (allocationPolicy) { case HEAP_MEMORY: buffer = ByteBuffer.allocate(size).order(byteOrder); break; case DIRECT_MEMORY: buffer = ByteBuffer.allocateDirect(size).order(byteOrder); break; case MMAPPED_MEMORY: default: // expect that dirs are already created and initialized if (!mmapSessionDir.exists()) { throw new RuntimeException(mmapSessionDir.getAbsolutePath() + " doesn't exist"); } if (restoreBuffers) { if (!mmapFile.exists()) { LOG.warn("restoreBuffers is true, but file " + mmapFile + " doesn't exist"); } else { LOG.info("restoring buffer from " + mmapFile); } } else { if (mmapFile.exists()) { // this path should never happen (only if the generated session ID accidentally matches a previous one) LOG.info("restoreBuffers is false; deleting existing mmap file " + mmapFile); if (!mmapFile.delete()) { throw new RuntimeException("deletion of file failed: " + mmapFile.getAbsolutePath()); } } LOG.info("restoreBuffers is false => will delete new mmap file " + mmapFile + " on exit"); mmapFile.deleteOnExit(); // in case we don't need files later. } try { FileChannel rwChannel = new RandomAccessFile(mmapFile, "rw").getChannel(); buffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, size).order(byteOrder); rwChannel.close(); } catch (FileNotFoundException e) { throw new RuntimeException( "[should never happen!] can't find mmap file/dir " + mmapFile.getAbsolutePath(), e); } catch (IOException e) { throw new RuntimeException("unable to initialize mmap file " + mmapFile, e); } } return buffer; }
From source file:com.almalence.opencam.SavingService.java
@TargetApi(21) private void saveDNGPicture(int frameNum, long sessionID, OutputStream os, int width, int height, int orientation, boolean cameraMirrored) { DngCreator creator = new DngCreator(CameraController.getCameraCharacteristics(), PluginManager.getInstance().getFromRAWCaptureResults("captureResult" + frameNum + sessionID)); byte[] frame = SwapHeap.SwapFromHeap( Integer.parseInt(getFromSharedMem("resultframe" + frameNum + Long.toString(sessionID))), Integer.parseInt(getFromSharedMem("resultframelen" + frameNum + Long.toString(sessionID)))); ByteBuffer buff = ByteBuffer.allocateDirect(frame.length); buff.put(frame);/*from w w w . j a v a2 s.c om*/ int exif_orientation = ExifInterface.ORIENTATION_NORMAL; switch ((orientation + 360) % 360) { default: case 0: exif_orientation = ExifInterface.ORIENTATION_NORMAL; break; case 90: exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_270 : ExifInterface.ORIENTATION_ROTATE_90; break; case 180: exif_orientation = ExifInterface.ORIENTATION_ROTATE_180; break; case 270: exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_90 : ExifInterface.ORIENTATION_ROTATE_270; break; } try { creator.setOrientation(exif_orientation); creator.writeByteBuffer(os, new Size(width, height), buff, 0); } catch (IOException e) { creator.close(); e.printStackTrace(); Log.e("Open Camera", "saveDNGPicture error: " + e.getMessage()); } creator.close(); }