/** * Creates a new instance that fetches data from the specified file. * * @param chunkSize the number of bytes to fetch on each * {@link #readChunk(ChannelHandlerContext)} call */ public ChunkedNioFile(File in, int chunkSize) throws IOException { this(new FileInputStream(in).getChannel(), chunkSize); }
public static void copyFile( File from, File to ) throws IOException { if ( !to.exists() ) { to.createNewFile(); } try ( FileChannel in = new FileInputStream( from ).getChannel(); FileChannel out = new FileOutputStream( to ).getChannel() ) { out.transferFrom( in, 0, in.size() ); } }
private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws IOException { FileChannel channel = fis.getChannel(); int fileSize = (int) channel.size(); ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize); channel.read(byteBuffer); byteBuffer.flip(); byte[] bytes = byteBuffer.array(); byteBuffer.clear(); channel.close(); fis.close(); return bytes; }
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); 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(); } }
lastPosition.metricFileName = null; lastPosition.indexFileName = null; if (!new File(idxFileName).exists()) { return -1; FileInputStream in = new FileInputStream(idxFileName); in.getChannel().position(offsetInIndex); DataInputStream indexIn = new DataInputStream(in); long offset; try { long second; lastPosition.offsetInIndex = in.getChannel().position(); while ((second = indexIn.readLong()) < beginSecond) { offset = indexIn.readLong(); lastPosition.offsetInIndex = in.getChannel().position();
public FileDataSourceViaHeapImpl(String f) throws FileNotFoundException { File file = new File(f); this.fc = new FileInputStream(file).getChannel(); this.filename = file.getName(); }
File chunkFile = new File(tempFolder, Integer.toString(chunkId)); chunkFile.createNewFile(); FileOutputStream stream = new FileOutputStream(chunkFile); dataFiles.add(new LocalDataFileChunk(new FileInputStream(chunkFile).getChannel())); dataFileSizes.add((int) chunkFile.length());
int showLogLength = Integer.parseInt(str[0]); if (file != null && file.exists()) { try { FileInputStream fis = new FileInputStream(file); FileChannel filechannel = fis.getChannel(); size = filechannel.size(); ByteBuffer bb; if (size <= showLogLength) {
public static String getFileMD5(File file) { if (!file.exists()) { return ""; } FileInputStream in = null; try { in = new FileInputStream(file); FileChannel channel = in.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); return bytes2Hex(md.digest()); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { } } } return ""; }
public static long sizeViaChannel(String fname) throws IOException { FileInputStream fis = new FileInputStream(fname); FileChannel ch = fis.getChannel(); return ch.size(); // nothing escapes, probably should report }
/** * 拷贝文件流,使用NIO * * @param in 输入 * @param out 输出 * @return 拷贝的字节数 * @throws IORuntimeException IO异常 */ public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException { Assert.notNull(in, "FileInputStream is null!"); Assert.notNull(out, "FileOutputStream is null!"); final FileChannel inChannel = in.getChannel(); final FileChannel outChannel = out.getChannel(); try { return inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw new IORuntimeException(e); } }
public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } }
public static void main(String[] args) throws IOException { System.out.println("PWD: " + System.getProperty("user.dir")); System.out.println("Input: " + args[0]); FileInputStream fis = new FileInputStream(new File(args[0])); System.out.println("input size: " + fis.getChannel().size()); PrintStructure ps = new PrintStructure(); ps.print(fis.getChannel(), 0, 0, 0); }
if (!new File(lastPosition.indexFileName).exists()) { return false; in = new FileInputStream(lastPosition.indexFileName); in.getChannel().position(lastPosition.offsetInIndex); DataInputStream indexIn = new DataInputStream(in);
public FileDataSourceImpl(String f) throws FileNotFoundException { File file = new File(f); this.fc = new FileInputStream(file).getChannel(); this.filename = file.getName(); }
int showLogLength = Integer.parseInt(str[0]); if (file != null && file.exists()) { try { FileInputStream fis = new FileInputStream(file); FileChannel filechannel = fis.getChannel(); size = filechannel.size(); ByteBuffer bb; if (size <= showLogLength) {
File jobLog = getJobLog(); launchCount = 0; if(!jobLog.exists()) return; startPosition = jobLog.length()-(FileUtils.ONE_KB * 100); FileInputStream jobLogIn = new FileInputStream(jobLog); jobLogIn.getChannel().position(startPosition); BufferedReader jobLogReader = new BufferedReader( new InputStreamReader(jobLogIn));
/** * Creates a new instance that fetches data from the specified file. */ public ChunkedNioFile(File in) throws IOException { this(new FileInputStream(in).getChannel()); }
@NoWarning("OBL_UNSATISFIED_OBLIGATION,OS_OPEN_STREAM") public static long sizeViaChannelCloseDoNotReport(String fname) throws IOException { FileInputStream fis = new FileInputStream(fname); FileChannel ch = fis.getChannel(); long sz = ch.size(); // fis.close(); ch.close(); return sz; }
@Override public byte[] read() throws IOException { Closer closer = Closer.create(); try { FileInputStream in = closer.register(openStream()); return ByteStreams.toByteArray(in, in.getChannel().size()); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }