List of usage examples for java.nio.channels FileChannel map
public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;
From source file:Main.java
public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) { try {/* w w w .j av a2 s. c o m*/ // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. int index = tempFileName.lastIndexOf("."); if (index != -1) tempFileName = tempFileName.substring(0, index); File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Config type = srcBitmap.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height); srcBitmap.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. srcBitmap.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. srcBitmap = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary srcBitmap.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return srcBitmap; }
From source file:Main.java
public static ByteBuffer fromFile(File file) throws IOException { RandomAccessFile raf = null;// w w w . ja v a 2 s. c o m FileChannel channel = null; try { raf = new RandomAccessFile(file, "r"); channel = raf.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()).load(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { // Ignored. } } if (raf != null) { try { raf.close(); } catch (IOException e) { // Ignored. } } } }
From source file:Main.java
/** Copia un fichero. * @param source Fichero origen con el contenido que queremos copiar. * @param dest Fichero destino de los datos. * @throws IOException SI ocurre algun problema durante la copia */ public static void copyFile(final File source, final File dest) throws IOException { if (source == null || dest == null) { throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$ }// www . j av a 2 s . c om final FileInputStream is = new FileInputStream(source); final FileOutputStream os = new FileOutputStream(dest); final FileChannel in = is.getChannel(); final FileChannel out = os.getChannel(); final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); in.close(); out.close(); is.close(); os.close(); }
From source file:com.twitter.ambrose.util.JSONUtil.java
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {/*from w w w . j a v a 2s . c om*/ FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:org.springframework.hateoas.VndErrorsMarshallingTest.java
private static String readFile(org.springframework.core.io.Resource resource) throws IOException { FileInputStream stream = new FileInputStream(resource.getFile()); try {/*from ww w. j a va2s . c o m*/ FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:edu.isi.webserver.FileUtil.java
public static String readFileContentsToString(File file) throws IOException { FileInputStream stream = new FileInputStream(file); try {// w w w . jav a 2s . c o m FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:Main.java
private static byte[] readFile2Bytes(final File file) { FileChannel fc = null; try {/*ww w . j av a 2 s. c o m*/ fc = new RandomAccessFile(file, "r").getChannel(); int size = (int) fc.size(); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load(); byte[] data = new byte[size]; mbb.get(data, 0, size); return data; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (fc != null) { fc.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.xlcloud.service.manager.VirtualClusterTemplateManagerImpl.java
private static String readFile(String path) throws IOException, FileNotFoundException { FileInputStream stream = new FileInputStream(new File(path)); try {/*ww w . j a va2 s . c om*/ FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:GrepSun.java
private static void grep(File f) throws IOException { // Open the file and then get a channel from the stream FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer CharBuffer cb = decoder.decode(bb); // Perform the search grep(f, cb);/*w w w . j a va2 s .c om*/ // Close the channel and the stream fc.close(); }
From source file:edu.usf.cutr.siri.SiriParserJacksonExample.java
/** * Read in input file to string/*from w w w .jav a 2 s . c o m*/ * @param file file containing the JSON or XML data * @return String representation of the JSON or XML file * @throws IOException */ private static String readFile(File file) throws IOException { FileInputStream stream = new FileInputStream(file); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }