List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
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 {//from w w w. java2 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:org.apache.hadoop.hdfs.client.ClientMmap.java
public static ClientMmap load(ClientMmapManager manager, FileInputStream in, ExtendedBlock block, DatanodeID datanodeID) throws IOException { MappedByteBuffer map = in.getChannel().map(MapMode.READ_ONLY, 0, in.getChannel().size()); return new ClientMmap(manager, map, block, datanodeID); }
From source file:Main.java
public static File copyFileTo(File src, File dst) { FileChannel in = null;//from w w w.j a va 2s .c om FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(src); outStream = new FileOutputStream(dst); in = inStream.getChannel(); out = outStream.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (inStream != null) { inStream.close(); } if (outStream != null) { outStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return dst; }
From source file:Main.java
/** * This method handels the reading of data from a specific file. * // w w w . j ava 2 s. c o m * @param file the file, to read from. * @param blockSize the length of the data-block, which should be read from the specified file. * @return the data read from the file. */ public static byte[] readFile(File file, long blockSize) { FileInputStream fis; byte[] fileContent = null; try { fis = new FileInputStream(file); FileChannel fileChannel = fis.getChannel(); int bytesToRead; fileChannel.position(readStreamPosition); int dataLen = fis.available(); // if there is a zero block size specified, read the whole file if (blockSize == 0L) { bytesToRead = dataLen; } else { bytesToRead = (int) blockSize; } fileContent = new byte[bytesToRead]; // reading the data for (int i = 0; i < bytesToRead; i++) { fileContent[i] = (byte) fis.read(); } // storing read-position readStreamPosition = fileChannel.position(); fis.close(); fileChannel.close(); // zero blockSize indicates, that reading of this file is completed, // stream position reset if (blockSize == 0L) { readStreamPosition = 0L; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fileContent; }
From source file:edu.usf.cutr.siri.SiriParserJacksonExample.java
/** * Read in input file to string//from w w w .j av a 2s. 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(); } }
From source file:eu.databata.engine.util.PropagationUtils.java
/** * This method is suitable for reading files not larger than 2 GB. *//*from w w w. j av a2 s. c om*/ private static String readFile(File file, String encoding) { String result = null; try { FileInputStream inputStream = new FileInputStream(file); try { byte[] bytes = new byte[(int) inputStream.getChannel().size()]; inputStream.read(bytes); result = Charset.forName(encoding).decode(ByteBuffer.wrap(bytes)).toString(); } finally { inputStream.close(); } } catch (Exception e) { LOG.warn("Failed to read file: " + file.getName()); throw new RuntimeException(e); } return dos2Unix(result); }
From source file:ch.admin.suis.msghandler.util.ZipUtils.java
/** * Creates a new unique ZIP file in the destination directory and adds to it * the provided collection of files.// w w w . ja v a 2 s . c om * * @param toDir The name of the file created * @param files The files to compress * @return A Zipped File * @throws IOException if the file cannot be created because of a IO error */ public static File compress(File toDir, Collection<File> files) throws IOException { final File zipFile = File.createTempFile("data", ".zip", toDir); // was there an exception? boolean exceptionThrown = false; try (ZipOutputStream zout = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(zipFile), BUFFER_SIZE))) { byte[] data = new byte[BUFFER_SIZE]; for (File file : files) { // create the entry zout.putNextEntry(new ZipEntry(file.getName())); FileInputStream in = new FileInputStream(file); try (FileLock lock = in.getChannel().tryLock(0, Long.MAX_VALUE, true)) { isInValid(lock, in); int len; // write the file to the entry while ((len = in.read(data)) > 0) { zout.write(data, 0, len); } lock.release(); } finally { try { in.close(); } catch (IOException e) { LOG.error("cannot properly close the opened file " + file.getAbsolutePath(), e); } } } } catch (IOException e) { LOG.error("error while creating the ZIP file " + zipFile.getAbsolutePath(), e); // mark for the finally block exceptionThrown = true; // rethrow - the finally block is only for the first exception throw e; } finally { // remove the file in case of an exception if (exceptionThrown && !zipFile.delete()) { LOG.error("cannot delete the file " + zipFile.getAbsolutePath()); } } return zipFile; }
From source file:org.jasig.portlet.attachment.util.FileUtil.java
public static byte[] read(File file) throws IOException { if (file == null || !file.exists()) return null; if (locks.contains(file.getAbsolutePath())) { try {/*from w w w . ja va2 s . c o m*/ Thread.sleep(100); } catch (InterruptedException ie) { } } FileInputStream input = null; try { input = new FileInputStream(file); int available = input.available(); FileChannel channel = input.getChannel(); ByteBuffer bytes = ByteBuffer.allocate(available); channel.read(bytes); bytes.flip(); return bytes.array(); } finally { if (input != null) { input.close(); } } }
From source file:edu.umich.flowfence.common.ParceledPayload.java
public static ParceledPayload fromParcel(Parcel p) { byte[] data = p.createByteArray(); if (data == null) { // Null data = data's stored in an ashmem region. try (ParcelFileDescriptor pfd = p.readFileDescriptor()) { FileDescriptor fd = pfd.getFileDescriptor(); int size = MemoryFile.getSize(fd); if (size == -1) { throw new ParcelFormatException("ParceledPayload blob is not ashmem"); }//from w w w. j a v a2 s. co m data = new byte[size]; FileInputStream fis = new FileInputStream(fd); FileChannel chan = fis.getChannel(); MappedByteBuffer mapping = chan.map(FileChannel.MapMode.READ_ONLY, 0, size); mapping.get(data); } catch (IOException e) { Log.e(TAG, "Couldn't unparcel - not an ashmem region?", e); ParcelFormatException pfe = new ParcelFormatException("Exception reading blob for ParceledPayload"); pfe.initCause(e); throw pfe; } } return new ParceledPayload(data); }
From source file:Main.java
public static boolean copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();//from w ww . j a v a 2s. co m } FileInputStream source = null; FileOutputStream destination = null; try { source = new FileInputStream(sourceFile); destination = new FileOutputStream(destFile); destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size()); } catch (Exception e) { // FileLog.e("tmessages", e); return false; } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } return true; }