List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA3470.java
/** * Copy the given source file to the given destination * * @param sourceFile// w w w . ja va 2 s. c om * source file * @param destFile * destination file * @throws IOException */ public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(destFile); FileChannel source = fileInputStream.getChannel(); FileChannel destination = fileOutputStream.getChannel(); destination.transferFrom(source, 0, source.size()); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(fileOutputStream); } }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void mappedTest(File source, File target) throws Exception { FileInputStream fis = null; FileOutputStream fos = null;/*from ww w . ja va 2 s . c om*/ MappedByteBuffer mapbuffer = null; try { long fileSize = source.length(); final byte[] outputData = new byte[(int) fileSize]; fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); target.createNewFile(); mapbuffer = sChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize); for (int i = 0; i < fileSize; i++) { outputData[i] = mapbuffer.get(); } mapbuffer.clear(); fos.write(outputData); fos.flush(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); if (mapbuffer == null) { return; } final Object buffer = mapbuffer; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Method clean = buffer.getClass().getMethod("cleaner", new Class[0]); if (clean == null) { return null; } clean.setAccessible(true); sun.misc.Cleaner cleaner = (sun.misc.Cleaner) clean.invoke(buffer, new Object[0]); cleaner.clean(); } catch (Throwable ex) { } return null; } }); } }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.FileCopier.java
/** * Copies the contents of a file into a new file in the given directory. The new file will have the same name as * the original file.//from w w w .j a v a 2 s .com * * @param fromFile the file to copy * @param deployDirectory the directory in which to copy the file -- if this is not a directory, the new file will * be created with this name * * @return the File that was created * * @throws IOException if there are I/O errors during reading or writing */ public static File copy(final File fromFile, final File deployDirectory) throws IOException { // if not a directory, then just use deployDirectory as the filename for the copy File destination = deployDirectory; if (deployDirectory.isDirectory()) { // otherwise use original file's name destination = new File(deployDirectory, fromFile.getName()); } FileInputStream fileInputStream = null; FileChannel sourceChannel = null; FileOutputStream fileOutputStream = null; FileChannel destinationChannel = null; try { //noinspection IOResourceOpenedButNotSafelyClosed fileInputStream = new FileInputStream(fromFile); //noinspection ChannelOpenedButNotSafelyClosed sourceChannel = fileInputStream.getChannel(); //noinspection IOResourceOpenedButNotSafelyClosed fileOutputStream = new FileOutputStream(destination); //noinspection ChannelOpenedButNotSafelyClosed destinationChannel = fileOutputStream.getChannel(); final int maxCount = (64 * 1024 * 1024) - (32 * 1024); final long size = sourceChannel.size(); long position = 0; while (position < size) { position += sourceChannel.transferTo(position, maxCount, destinationChannel); } } catch (IOException ie) { throw new IOException("Failed to copy " + fromFile + " to " + deployDirectory + ".\n Error Details: " + ie.toString()); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(sourceChannel); IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(destinationChannel); } return destination; }
From source file:org.apache.pig.pigunit.PigTest.java
private static String readFile(File file) throws IOException { FileInputStream stream = new FileInputStream(file); try {//from www . j a va 2s . co 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:com.log4ic.compressor.utils.FileUtils.java
/** * ?/* ww w. ja v a 2 s .co m*/ * * @param fileInputStream * @return */ public static String readFile(FileInputStream fileInputStream) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); StringBuffer contentBuffer = new StringBuffer(); Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { FileChannel channel = fileInputStream.getChannel(); while (true) { buffer.clear(); int pos = channel.read(buffer); if (pos == -1) { break; } buffer.flip(); charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); contentBuffer.append(charBuffer.toString()); } } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return contentBuffer.toString(); }
From source file:de.wikilab.android.friendica01.Max.java
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {//w ww . j a v a 2 s . c om 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:tudarmstadt.lt.ABSentiment.featureExtractor.util.GloVeSpace.java
/** * Reads a binary model//w w w .java 2 s . com * @param vocabFile vocabulary file, containing all the words in the binary file * @param gloVeModel path containing the binary model * @param norm specifies if the word representation is to be normalised * @return a model containing the word representation of all the words in the vocabulary */ public static GloVeSpace load(String vocabFile, String gloVeModel, boolean norm) { GloVeSpace model = new GloVeSpace(); try { FileInputStream in = new FileInputStream(gloVeModel); DataInputStream ds = new DataInputStream(new BufferedInputStream(in, 131072)); List<String> vocab = FileUtils.readLines(new File(vocabFile)); long numWords = vocab.size(); // Vector Size = num of bytes in total / 16 / vocab int vecSize = (int) (in.getChannel().size() / 16 / numWords) - 1; // Word Vectors: for (String aVocab : vocab) { String word = StringUtils.split(aVocab, ' ')[0]; float[] vector = readFloatVector(ds, vecSize); model.store.put(word, new FloatMatrix(vector)); } // Unit Vectors: if (norm) { for (Entry<String, FloatMatrix> e : model.store.entrySet()) { model.store.put(e.getKey(), VectorMath.normalize(e.getValue())); } } vectorLength = vecSize; System.out.println(String.format("Loaded %s words, vector size %s", numWords, vecSize)); } catch (IOException e) { System.err.println("ERROR: Failed to load model: " + gloVeModel); e.printStackTrace(); } return model; }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java
/** * Return the length of bytes in the given file after subtracting * the trailer of 0xFF (OP_INVALID)s.//from ww w .j av a 2 s .c om * This seeks to the end of the file and reads chunks backwards until * it finds a non-0xFF byte. * @throws IOException if the file cannot be read */ private static long getNonTrailerLength(File f) throws IOException { final int chunkSizeToRead = 256 * 1024; FileInputStream fis = new FileInputStream(f); try { byte buf[] = new byte[chunkSizeToRead]; FileChannel fc = fis.getChannel(); long size = fc.size(); long pos = size - (size % chunkSizeToRead); while (pos >= 0) { fc.position(pos); int readLen = (int) Math.min(size - pos, chunkSizeToRead); IOUtils.readFully(fis, buf, 0, readLen); for (int i = readLen - 1; i >= 0; i--) { if (buf[i] != FSEditLogOpCodes.OP_INVALID.getOpCode()) { return pos + i + 1; // + 1 since we count this byte! } } pos -= chunkSizeToRead; } return 0; } finally { fis.close(); } }
From source file:org.jboss.as.forge.util.Files.java
/** * Copies the source file to the destination file. * * @param srcFile the file to copy/*from w w w .ja v a 2s.com*/ * @param targetFile the target file * * @return {@code true} if the file was successfully copied, {@code false} if the copy failed or was incomplete * * @throws IOException if an IO error occurs copying the file */ public static boolean copyFile(final File srcFile, final File targetFile) throws IOException { FileInputStream in = null; FileOutputStream out = null; FileChannel inChannel = null; FileChannel outChannel = null; try { in = new FileInputStream(srcFile); inChannel = in.getChannel(); out = new FileOutputStream(targetFile); outChannel = out.getChannel(); long bytesTransferred = 0; while (bytesTransferred < inChannel.size()) { bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); } } finally { Streams.safeClose(outChannel); Streams.safeClose(out); Streams.safeClose(inChannel); Streams.safeClose(in); } return srcFile.length() == targetFile.length(); }
From source file:org.srlutils.Files.java
/** load an object from file name, the returned size seems to be right, but i don't see it in the java spec */ public static DiskObject load(String name) { FileInputStream fin = null; ObjectInputStream in = null;// w ww .ja v a2s . c o m Object obj = null; long size = 0; RuntimeException rte = null; try { fin = new FileInputStream(name); in = new ObjectInputStream(fin); obj = in.readObject(); size = fin.getChannel().position(); // hack::undocumented -- is this in the javadocs ? } catch (ClassNotFoundException ex) { rte = rte(ex, "could not instatiate object from file: %s", name); } catch (IOException ex) { rte = rte(ex, "could not read object from file: %s", name); } finally { try { in.close(); fin.close(); } catch (Exception ex) { } } if (rte != null) throw rte; return new DiskObject(obj, name, size); }