List of usage examples for java.nio.channels FileChannel read
public final long read(ByteBuffer[] dsts) throws IOException
From source file:com.arc.embeddedcdt.gui.jtag.ConfigJTAGTab.java
public static byte[] read(File file) { try {/* w w w . ja va2 s. co m*/ FileInputStream fis = new FileInputStream(file); FileChannel fc = fis.getChannel(); byte[] data = new byte[(int) fc.size()]; // fc.size returns the // size of the file // which backs the // channel ByteBuffer bb = ByteBuffer.wrap(data); fc.read(bb); return data; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.bittorrent.mpetazzoni.common.Torrent.java
private static String hashFiles(List<File> files) throws InterruptedException, IOException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(Torrent.PIECE_LENGTH); List<Future<String>> results = new LinkedList<Future<String>>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / Torrent.PIECE_LENGTH)) }); length += file.length();//from w w w .j a va 2s . c o m FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { logger.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / Torrent.PIECE_LENGTH)); logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:com.turn.ttorrent.common.Torrent.java
private static String hashFiles(List<File> files, int pieceLenght) throws InterruptedException, IOException, NoSuchAlgorithmException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(pieceLenght); List<Future<String>> results = new LinkedList<Future<String>>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / pieceLenght)) }); length += file.length();//from w w w . jav a 2s. c o m FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { logger.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / pieceLenght)); logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:org.mhisoft.common.util.FileUtils.java
/** * @param source// w ww . java2s . co m * @param target * @throws IOException */ public static void copyFile(final File source, final File target) throws IOException { FileChannel in = null; FileChannel out = null; // long totalFileSize = 0; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(target).getChannel(); //totalFileSize = in.size(); ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER); int readSize = in.read(buffer); long totalRead = 0; //int progress = 0; //long startTime, endTime ; while (readSize != -1) { //startTime = System.currentTimeMillis(); totalRead = totalRead + readSize; //progress = (int) (totalRead * 100 / totalFileSize); buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer); //System.out.printf("."); //showPercent(rdProUI, totalSize/size ); } buffer.clear(); readSize = in.read(buffer); //endTime = System.currentTimeMillis(); } } finally { close(in); close(out); } }
From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java
@SuppressWarnings("resource") public static byte[] readFromFile(File file) throws IOException { FileChannel fileChannel = new FileInputStream(file).getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fileChannel.size()); fileChannel.read(bb); fileChannel.close();//w ww . ja v a2 s .co m bb.flip(); byte[] bytes; if (bb.hasArray()) { bytes = bb.array(); } else { bytes = new byte[bb.limit()]; bb.get(bytes); } return bytes; }
From source file:ga.rugal.jpt.common.tracker.common.Torrent.java
private static String hashFiles(List<File> files, int pieceLenght) throws InterruptedException, IOException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(pieceLenght); List<Future<String>> results = new LinkedList<>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { LOG.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / pieceLenght)) }); length += file.length();/* ww w . ja va 2 s. c o m*/ FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { LOG.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / pieceLenght)); LOG.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:com.p2p.peercds.common.Torrent.java
private static String hashFiles(List<File> files) throws InterruptedException, IOException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(PIECE_LENGTH); List<Future<String>> results = new LinkedList<Future<String>>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / PIECE_LENGTH)) }); length += file.length();// w w w .ja v a 2s . c o m FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { logger.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / PIECE_LENGTH)); logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:com.kactech.otj.Utils.java
public static byte[] readBytes(File f) throws IOException { FileInputStream fis = new FileInputStream(f); FileChannel fChannel = fis.getChannel(); byte[] barray = new byte[(int) f.length()]; ByteBuffer bb = ByteBuffer.wrap(barray); //bb.order(ByteOrder.LITTLE_ENDIAN); fChannel.read(bb); fChannel.close();//from w w w .jav a 2s .com fis.close(); return bb.array(); }
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * readFile/*from www .j av a2 s. c o m*/ * * @param filePath ? * @return byte[] */ public static byte[] readFile(String filePath) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "readFile", "[filePath:" + filePath + "]")); if (StringUtils.isEmpty(filePath)) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]")); throw new IllegalArgumentException(); } File file = new File(filePath); FileInputStream fin = null; FileChannel fcin = null; ByteBuffer rbuffer = ByteBuffer.allocate(TesseractConstant.FILE_BLOCK_SIZE); ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (file.exists()) { try { fin = new FileInputStream(file); fcin = fin.getChannel(); while (fcin.read(rbuffer) != -1) { bos.write(rbuffer.array()); } } catch (Exception e) { LOGGER.error(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]"), e); } finally { try { if (fin != null) { fin.close(); } if (fcin != null) { fcin.close(); } bos.close(); } catch (Exception e) { LOGGER.error(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "readFile", "[filePath:" + filePath + "]"), e); } } } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "readFile", "[filePath:" + filePath + "]")); return bos.toByteArray(); }
From source file:org.solmix.commons.util.Files.java
/** * ?/* ww w .j a v a 2 s. co m*/ * * @param f1 * ? * @param f2 * * @throws Exception */ public static void copyFile(File f1, File f2) throws Exception { int length = 2097152; FileInputStream in = new FileInputStream(f1); FileOutputStream out = new FileOutputStream(f2); FileChannel inC = in.getChannel(); FileChannel outC = out.getChannel(); ByteBuffer b = null; while (true) { if (inC.position() == inC.size()) { inC.close(); outC.close(); } if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else length = 2097152; b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } }