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; }
final FileChannel channel = new FileInputStream(fileName).getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); // when finished channel.close();
private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException { ensureAccessible(); if (length == 0) { return 0; } ByteBuffer tmpBuf = internal ? internalNioBuffer() : buffer.duplicate(); tmpBuf.clear().position(index).limit(index + length); return out.write(tmpBuf, position); }
@Override public long read(Buffer sink, long byteCount) throws IOException { if (!channel.isOpen()) throw new IllegalStateException("closed"); if (position == channel.size()) return -1L; long read = channel.transferTo(position, byteCount, sink); position += read; return read; }
/** * Attempt to read a file as a string * @throws IOException */ public static String readFileAsString(String path, Charset charset) throws IOException { if (charset == null) charset = Charset.defaultCharset(); try (FileChannel fc = FileChannel.open(Paths.get(path))) { MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); } }
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() ); } }
URL website = new URL("http://www.website.com/information.asp"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream("information.html"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
/** * @param igniteUrl Url to ignite. * @return Ignite file name. */ private String downloadIgnite(String igniteUrl) { try { URL url = new URL(igniteUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { String fileName = fileName(url.toString()); String filePath = props.igniteLocalWorkDir() + File.separator + fileName; if (new File(filePath).exists()) return fileName; FileOutputStream outFile = new FileOutputStream(filePath); outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE); outFile.close(); return fileName; } else throw new RuntimeException("Got unexpected response code. Response code: " + code); } catch (IOException e) { throw new RuntimeException("Failed update ignite.", e); } }
public void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); }
public static void copyFdToFile(FileDescriptor src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
public static void main(String[] args) throws IOException { // trying FileLock mechanics in different processes File file = new File("tmp.lock"); file.createNewFile(); FileChannel channel = new RandomAccessFile(file, "r").getChannel(); boolean shared = true; FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared); System.out.println("locked " + lock1); System.in.read(); System.out.println("release " + lock1); lock1.release(); }
public static void writeToFile(byte[] data, File target) throws IOException { FileOutputStream fo = null; ReadableByteChannel src = null; FileChannel out = null; try { src = Channels.newChannel(new ByteArrayInputStream(data)); fo = new FileOutputStream(target); out = fo.getChannel(); out.transferFrom(src, 0, data.length); } finally { if (fo != null) { fo.close(); } if (src != null) { src.close(); } if (out != null) { out.close(); } } }
public synchronized void writeToConfigXmlFile(String content) { FileChannel channel = null; FileOutputStream outputStream = null; FileLock lock = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw"); channel = randomAccessFile.getChannel(); lock = channel.lock(); randomAccessFile.seek(0); randomAccessFile.setLength(0); outputStream = new FileOutputStream(randomAccessFile.getFD()); IOUtils.write(content, outputStream, UTF_8); } catch (Exception e) { throw new RuntimeException(e); } finally { if (channel != null && lock != null) { try { lock.release(); channel.close(); IOUtils.closeQuietly(outputStream); } catch (IOException e) { LOGGER.error("Error occured when releasing file lock and closing file.", e); } } } }
public void seek(long seekBytes) throws FileNotFoundException, IOException, InterruptedException { fileInput = new FileInputStream(file); fileChannel = fileInput.getChannel(); try { fileChannel.position(seekBytes); } catch (ClosedByInterruptException e) { throw new InterruptedException(); } bufferedInput = new BufferedInputStream(fileInput, size); dataInput = new DataInputStream(bufferedInput); offset = seekBytes; }
void addPatchFile(String zipFileName) throws IOException { File file = new File(zipFileName); RandomAccessFile f = new RandomAccessFile(file, "r"); long fileLength = f.length(); f.close(); throw new java.io.IOException(); MappedByteBuffer directoryMap = f.getChannel().map( FileChannel.MapMode.READ_ONLY, dirOffset, dirSize); directoryMap.order(ByteOrder.LITTLE_ENDIAN); if (directoryMap.getInt(currentOffset) != kCDESignature) { Log.w(LOG_TAG, "Missed a central dir sig (at " + currentOffset + ")"); .getShort(currentOffset + kCDENameLen) & 0xffff; int extraLen = directoryMap.getShort(currentOffset + kCDEExtraLen) & 0xffff; int commentLen = directoryMap.getShort(currentOffset
public static Slice mapFileReadOnly(File file) throws IOException { requireNonNull(file, "file is null"); if (!file.exists()) { throw new FileNotFoundException(file.toString()); } try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); FileChannel channel = randomAccessFile.getChannel()) { MappedByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, file.length()); return wrappedBuffer(byteBuffer); } } }
raf = new RandomAccessFile(file, "r"); channel = raf.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength).load(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { raf.close(); } catch (IOException e) {
public static MappedByteBuffer expandBuffer(String filename, MappedByteBuffer buffer, long newSize) { buffer.force(); int oldPosition = buffer.position(); try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) { raf.setLength(newSize); buffer = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); buffer.position(oldPosition); return buffer; } catch (IOException e) { throw new RuntimeException(e); } }
private static MappedByteBuffer mapInternal(File file, MapMode mode, long size) throws IOException { checkNotNull(file); checkNotNull(mode); Closer closer = Closer.create(); try { RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size == -1 ? channel.size() : size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }