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:net.simon04.guavavfs.VirtualFiles.java
private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException { Closer closer = Closer.create();//from ww w. j a v a 2s. co m try { FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.lenskit.data.packed.BinaryRatingDAO.java
/** * Open a binary rating DAO./*from w ww . j a v a 2 s .co m*/ * * @param file The file to open. * @return A DAO backed by {@code file}. * @throws IOException If there is */ public static BinaryRatingDAO open(File file) throws IOException { try (FileInputStream input = new FileInputStream(file)) { FileChannel channel = input.getChannel(); BinaryHeader header = BinaryHeader.read(channel); logger.info("Loading DAO with {} ratings of {} items from {} users", header.getRatingCount(), header.getItemCount(), header.getUserCount()); // the channel position has been advanced to end of header ByteBuffer data = channel.map(FileChannel.MapMode.READ_ONLY, channel.position(), header.getRatingDataSize()); channel.position(channel.position() + header.getRatingDataSize()); ByteBuffer tableBuffer = channel.map(FileChannel.MapMode.READ_ONLY, channel.position(), channel.size() - channel.position()); BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer); BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer); return new BinaryRatingDAO(file, header, data, utbl, itbl, header.getRatingCount(), Long.MAX_VALUE); } }
From source file:HSqlPrimerDesign.java
@Deprecated public static String readFile(String file) throws IOException { FileChannel inChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); char ch;/*from ww w . j ava2 s .com*/ StringBuffer line = new StringBuffer(); for (int i = 0; i < buffer.limit(); i++) { ch = ((char) buffer.get()); line.append(ch); } return line.toString(); }
From source file:marytts.util.io.FileUtils.java
public static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try {//from w w w .j a va 2s .c om System.out.println("copying: " + source + "\n --> " + dest); in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); } catch (Exception e) { System.out.println("Error copying file " + source.getAbsolutePath() + " to " + dest.getAbsolutePath() + " : " + e.getMessage()); throw new IOException(); } finally { FileUtils.close(in, out); } }
From source file:com.stimulus.archiva.store.MessageStore.java
public static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try {// w w w .java 2s . co m in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); long size = in.size(); MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); out.write(buf); } finally { if (in != null) in.close(); if (out != null) out.close(); } }
From source file:it.polito.tellmefirst.parsing.TXTparser.java
public String txtToText(File file) throws TMFVisibleException { LOG.debug("[txtToText] - BEGIN"); String result;//from w w w . ja va2 s .c o m try { FileInputStream stream = new FileInputStream(file); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); stream.close(); result = Charset.defaultCharset().decode(bb).toString(); } catch (Exception e) { LOG.error("[txtToText] - EXCEPTION: ", e); throw new TMFVisibleException( "Problem parsing the file: the TXT document you uploaded seems malformed."); } LOG.debug("[txtToText] - END"); return result; }
From source file:com.example.psumaps.MapView.java
public static Bitmap convertToMutable(Bitmap imgIn) { try {/*from w ww . ja va 2 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. File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.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 = imgIn.getWidth(); int height = imgIn.getHeight(); Bitmap.Config type = imgIn.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(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height); imgIn.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. imgIn.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. imgIn = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary imgIn.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 imgIn; }
From source file:org.squidy.designer.components.pdf.PDFPane.java
public PDFPane(String pdfFileLoc) { super();// w w w . j a va 2s. co m try { // load a pdf from a byte buffer File file = new File(pdfFileLoc); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); pdfComponents = new PagePanel[pdffile.getNumPages()]; pageHeight = (int) pdffile.getPage(0).getHeight() + 3; pageWidth = (int) pdffile.getPage(0).getWidth() + 1; for (int i = 0; i < pdfComponents.length; i++) { PagePanel p = new PagePanel(); p.setBackground(Color.BLACK); p.setPreferredSize(new Dimension(pageWidth, pageHeight)); PSwing ps = new PSwing(p); ps.setOffset(0, i * pageHeight); addChild(ps); p.showPage(pdffile.getPage(i)); } } catch (IOException e) { if (LOG.isErrorEnabled()) { LOG.error("Error in " + PDFPane.class.getName() + ".", e); } System.out.println(e.getMessage()); } }
From source file:org.lnicholls.galleon.util.Tools.java
public static InputStream getInputStream(File file) { try {//from w ww . j a v a 2 s . c om FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); final ByteBuffer buf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size()); return new InputStream() { public synchronized int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get(); } public synchronized int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; } catch (Exception ex) { Tools.logException(Tools.class, ex, file.getAbsolutePath()); } return null; }
From source file:org.lnicholls.galleon.util.Tools.java
public static BufferedImage ImageIORead(File file) { System.gc();/* w ww . ja v a 2 s . c o m*/ try { FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); final ByteBuffer buf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size()); return ImageIO.read(new InputStream() { public synchronized int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get(); } public synchronized int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }); } catch (Exception ex) { Tools.logException(Tools.class, ex, file.getAbsolutePath()); } try { return ImageIO.read(new FileInputStream(file)); } catch (Exception ex) { Tools.logException(Tools.class, ex, file.getAbsolutePath()); } return null; }