public static long copyFile(java.nio.file.Path srcfile, java.nio.file.Path dstfile) throws java.io.IOException { java.nio.channels.FileChannel inchan = java.nio.channels.FileChannel.open(srcfile, OPENOPTS_READ); java.nio.channels.FileChannel outchan = null; long nbytes = 0; long filesize; try { filesize = inchan.size(); outchan = java.nio.channels.FileChannel.open(dstfile, OPENOPTS_CREATE_WRITE_TRUNC); nbytes = outchan.transferFrom(inchan, 0, inchan.size()); } finally { try { inchan.close(); } catch (Exception ex) { } if (outchan != null) try { outchan.close(); } catch (Exception ex) { } } if (nbytes != filesize) throw new java.io.IOException( "file-copy=" + nbytes + " vs " + filesize + " for " + srcfile + " => " + dstfile); return nbytes; }
/** Creates an IndexInput for the file with the given name. */ @Override public IndexInput openInput(String name, IOContext context) throws IOException { ensureOpen(); File path = new File(getDirectory(), name); FileChannel fc = FileChannel.open(path.toPath(), StandardOpenOption.READ); return new NIOFSIndexInput("NIOFSIndexInput(path=\"" + path + "\")", fc, context); }
public static GLTexture load(Path thePath) { try { return load(FileChannel.open(thePath, StandardOpenOption.READ)); } catch (IOException e) { throw new RuntimeException(e); } }
/** Reply a specific WAL. */ private void replayLog(Path logPath) throws KevaDBException { // Now look at the disk. if (logPath != null) { // Get a list of the sstables already in place. Map<String, Integer> manifest = db.getDiskService().getDataManifests(db, 0, SSTableService.MAX_LEVELS); FileLock fileLock = null; try { // Must open in "rw" mode so that we can use filelock. FileChannel fc = FileChannel.open(logPath, StandardOpenOption.READ, StandardOpenOption.WRITE); MappedByteBuffer in = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); fileLock = fc.tryLock(); if (fileLock != null) { for (; ; ) { if (!unroll(in, manifest)) { break; } } } if (fileLock != null) { fileLock.release(); } fc.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * Creates a {@code PathPart} withe the given {@code name} and referencing the given file {@code * path}. * * <p>All the headers (apart {@code "Content-Disposition"}) are specified in the given {@code * fields} parameter. * * @param name the part name * @param path the file path representing the content * @param fields the headers associated with this part * @throws IOException if an error occurs while trying to open the file path */ public PathPart(String name, Path path, Fields fields) throws IOException { this.buffers.add(convertFields(name, path, fields)); this.path = path; this.channel = FileChannel.open(path, StandardOpenOption.READ); this.buffers.addAll(fileMap(channel)); this.length = buffers.stream().mapToInt(Buffer::remaining).sum(); }
public static void main(String[] args) throws MPIException { args = MPI.Init(args); Intracomm worldProcComm = MPI.COMM_WORLD; int worldProcRank = worldProcComm.getRank(); int worldProcsCount = worldProcComm.getSize(); int size = Integer.parseInt(args[0]); Range[] ranges = RangePartitioner.partition(size, worldProcsCount); Range myRange = ranges[worldProcRank]; String fname = "/dev/shm/mmap.tmp"; try (FileChannel fc = FileChannel.open( Paths.get(fname), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ)) { int extent = size * Double.BYTES; bytes = ByteBufferBytes.wrap(fc.map(FileChannel.MapMode.READ_WRITE, 0L, extent)); byteBuffer = bytes.sliceAsByteBuffer(byteBuffer); byteSlice = bytes.slice(myRange.getStartIndex() * Double.BYTES, myRange.getLength() * Double.BYTES); byteBufferSlice = byteSlice.sliceAsByteBuffer(byteBufferSlice); Win win = new Win(byteBuffer, extent, Double.BYTES, MPI.INFO_NULL, worldProcComm); for (int i = 0; i < myRange.getLength(); ++i) { byteSlice.writeDouble(i * Double.BYTES, worldProcRank); } win.fence(0); if (worldProcRank != 0) { win.put( byteBufferSlice, myRange.getLength(), MPI.DOUBLE, 0, myRange.getStartIndex(), myRange.getLength(), MPI.DOUBLE); } win.fence(0); worldProcComm.barrier(); if (worldProcRank == 0) { for (int i = 0; i < size; ++i) { System.out.println(bytes.readDouble(i * Double.BYTES)); } } worldProcComm.barrier(); } catch (IOException e) { e.printStackTrace(); } MPI.Finalize(); }
/** * Writes out an InputStream to a file on disk. * * @param srcStream Stream to read data from * @param targetFile File to which the stream will be written. * @return size of stream, measured in bytes * @throws IOException when unable to write to target file */ protected long saveStreamToFile(InputStream srcStream, Path targetFile) throws IOException { long position = 0; try (ReadableByteChannel srcChannel = Channels.newChannel(srcStream); FileChannel targetFileChannel = FileChannel.open(targetFile, StandardOpenOption.WRITE)) { long count = 0; while ((count = targetFileChannel.transferFrom(srcChannel, position, Long.MAX_VALUE)) > 0) { position += count; } } return position; }
/** seekableByteChannel 可以更灵活的操作苦文件的任何位置 */ @Test public void testSeek() { Path path2 = Paths.get(PATH); try { FileChannel channel = FileChannel.open(path2, StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); int read = channel.read(buffer, channel.size() - 1000); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public ImmutableTranslogReader openReader(Path path, long id) throws IOException { FileChannel channel = FileChannel.open(path, StandardOpenOption.READ); try { final ChannelReference raf = new ChannelReference(path, id, channel, null); ImmutableTranslogReader reader = ImmutableTranslogReader.open( raf, new Checkpoint(Files.size(path), TranslogReader.UNKNOWN_OP_COUNT, id), null); channel = null; return reader; } finally { IOUtils.close(channel); } }
public static void seekByChannel() { ByteBuffer buffer = ByteBuffer.allocate(1024); try { FileChannel channel = FileChannel.open(Paths.get("/var/log/daily.out"), StandardOpenOption.READ); channel.read(buffer, channel.size() - 1024); System.out.println("---" + (char) buffer.get()); } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } }
public static long checksumMappedFile(Path filename) throws IOException { try (FileChannel channel = FileChannel.open(filename)) { CRC32 crc = new CRC32(); int length = (int) channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length); for (int p = 0; p < length; p++) { int c = buffer.get(p); crc.update(c); } return crc.getValue(); } }
public void test() throws Exception { MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(10); Mailbox factoryMailbox = mailboxFactory.createMailbox(); JAFactory factory = new JAFactory(factoryMailbox); (new JFileFactories(factoryMailbox)).setParent(factory); NullTransactionFactory ntf = new NullTransactionFactory("n"); factory.registerActorFactory(ntf); JAFuture future = new JAFuture(); Mailbox dbMailbox = mailboxFactory.createAsyncMailbox(); StatelessDB db = new StatelessDB(dbMailbox); db.setParent(factory); TransactionProcessor transactionProcessor = new TransactionProcessor(dbMailbox); transactionProcessor.setParent(db); JFile jFile = new JFile(mailboxFactory.createAsyncMailbox()); jFile.setParent(transactionProcessor); Path path = FileSystems.getDefault().getPath("TransactionLoggerTimingTest.jf"); System.out.println(path.toAbsolutePath()); jFile.fileChannel = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); Mailbox transactionLoggerMailbox = mailboxFactory.createAsyncMailbox(); TransactionLogger3 transactionLogger = new TransactionLogger3(transactionLoggerMailbox); transactionLogger.setParent(jFile); transactionLogger.initialCapacity = 2000; TransactionLoggerDriver transactionLoggerDriver = new TransactionLoggerDriver(mailboxFactory.createAsyncMailbox()); transactionLoggerDriver.setParent(transactionLogger); transactionLoggerDriver.batch = 1; transactionLoggerDriver.count = 1; transactionLoggerDriver.win = 3; Go.req.send(future, transactionLoggerDriver); Go.req.send(future, transactionLoggerDriver); long t0 = System.currentTimeMillis(); Go.req.send(future, transactionLoggerDriver); long t1 = System.currentTimeMillis(); System.out.println("milliseconds: " + (t1 - t0)); System.out.println( "transactions: " + (transactionLoggerDriver.batch * transactionLoggerDriver.count)); // latency = 2 ms // throughput = 500,000 tps jFile.fileChannel.close(); mailboxFactory.close(); }
@Override public void download(String id, WritableByteChannel target) throws IOException { Path tmpFile = Files.createTempFile(tmpDir, id, ".unzipped"); try (FileChannel tmpChannel = FileChannel.open(tmpFile, FileUtils.TMP_FILE_OPEN_OPTIONS)) { underlyingStore.download(id, tmpChannel); // Reset channel for reading tmpChannel.position(0); GZIPInputStream in = new GZIPInputStream(Channels.newInputStream(tmpChannel)); OutputStream out = Channels.newOutputStream(target); IOUtils.copy(in, out); logger.debug("Data '{}' successfully unzipped", id); } }
private void combineChunk(Path downloadTo, Path path) throws Exception { final long start = System.currentTimeMillis(); long bytes = 0; log.info("Writing {} to {}", path, downloadTo); try (WritableByteChannel wbs = Files.newByteChannel( downloadTo, EnumSet.of(StandardOpenOption.APPEND, StandardOpenOption.WRITE))) { try (FileChannel readChannel = FileChannel.open( path, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE))) { bytes = readChannel.size(); readChannel.transferTo(0, bytes, wbs); } } log.info("Finished writing {} bytes in {}", bytes, JavaUtils.duration(start)); }
@Override public void upload(String id, ReadableByteChannel src, long length) throws IOException { Path tmpFile = Files.createTempFile(tmpDir, id, ".zipped"); try (FileChannel tmpChannel = FileChannel.open(tmpFile, FileUtils.TMP_FILE_OPEN_OPTIONS)) { InputStream in = Channels.newInputStream(src); GZIPOutputStream out = new GZIPOutputStream(Channels.newOutputStream(tmpChannel)); IOUtils.copy(in, out); out.finish(); logger.debug("Data '{}' successfully zipped", id); // Reset channel for reading tmpChannel.position(0); underlyingStore.upload(id, tmpChannel, tmpChannel.size()); } }
private static void bufferWrite(ByteBuffer buffer) throws IOException { try (FileChannel srcChannel = FileChannel.open(Files.createTempFile(TEMP, null), StandardOpenOption.WRITE)) { srcChannel.write(buffer); } }
public static void dump(String file) throws Exception { Path path = FileSystems.getDefault().getPath(file); Set<OpenOption> opts = new HashSet<OpenOption>(); opts.add(StandardOpenOption.READ); FileChannel fileChannel = FileChannel.open(path, opts); PEFormat peFormat = new PEFormat(); peFormat.readFrom(fileChannel); long peHeaderOffset = peFormat.getPEFileHeaderOffset(); System.out.println("File: '" + file + "'; Size: " + fileChannel.size()); { int x = PEFormat.DOSHeader.SIZE; long y = peHeaderOffset; System.out.println( "DOS Header size: " + x + "; PE Header offset: " + y + "; diff: " + (y - x)); } { long x = peHeaderOffset; int y = peFormat.getPEHeadersTotalSize(); System.out.println( "PE Header offset: " + x + "; PE header and optional header total size: " + y + "; PE Header ends at:" + (x + y)); } { long x = peFormat.getSectionHeadersOffset(); long y = peFormat.getSectionHeadersSize(); System.out.println( "PE Section headers start at: " + x + "; PE section headers total size: " + y + "; PE Section headers end at:" + (x + y)); } System.out.println("Raw data start at:" + peFormat.peOptionalHeader.SizeOfHeaders); List<PEFormat.SectionHeader> sections = peFormat.sections; System.out.println("Number of sections:" + sections.size()); for (PEFormat.SectionHeader sectionHeader : sections) { System.out.println( "Section name: " + new String(sectionHeader.Name) + "; raw data at: " + sectionHeader.PointerToRawData + "; raw data size: " + sectionHeader.SizeOfRawData + "; raw data ends at: " + (sectionHeader.PointerToRawData + sectionHeader.SizeOfRawData)); } List<String> verificationErrors = PEVerifier.validate(peFormat); System.out.println("Verification errors: " + verificationErrors.size()); }
private void restore(Path path, ByteBufferReplacer replacer) throws IOException { try (FileChannel channel = FileChannel.open(path, READ, WRITE)) { MappedByteBuffer buffer = channel.map(READ_WRITE, 0, channel.size()); restore(buffer, replacer); } }
/** {@inheritDoc} */ @Override public void write(GatheringByteChannel outputchannel) { int numimages = numImages(); // Build header ByteBuffer header = wsaheader.toByteBuffer(); // Build palette ByteBuffer palette = wsapalette.toByteBuffer(); // Encode each frame, construct matching offsets ByteBuffer[] frames = new ByteBuffer[isLooping() ? numimages + 1 : numimages]; ByteBuffer lastbytes = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( width() * height()); ByteBuffer frameoffsets = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( (numimages + 2) * 4); int offsettotal = WsaFileHeaderCNC.HEADER_SIZE + ((numimages + 2) * 4); for (int i = 0; i < frames.length; i++) { ByteBuffer framebytes = wsaframes[i]; ByteBuffer frameint = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( (int) (framebytes.capacity() * 1.5)); ByteBuffer frame = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( (int) (framebytes.capacity() * 1.5)); // First encode in Format40, then Format80 CodecUtility.encodeFormat40(framebytes, frameint, lastbytes); CodecUtility.encodeFormat80(frameint, frame); frames[i] = frame; lastbytes = framebytes; frameoffsets.putInt(offsettotal); offsettotal += frame.limit(); } // Last offset for EOF frameoffsets.putInt(offsettotal); frameoffsets.rewind(); // Write file to disk try { outputchannel.write(header); outputchannel.write(frameoffsets); outputchannel.write(palette); outputchannel.write(frames); } catch (IOException e) { throw new RuntimeException(e); } // Generate high-res colour lookup table if (!srcnohires) { // Figure-out the appropriate file name String lookupname = filename.contains(".") ? filename.substring(0, filename.lastIndexOf('.')) + ".pal" : filename + ".pal"; // Write the index of the closest interpolated palette colour // TODO: Perform proper colour interpolation ByteBuffer lookup = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer(256); for (int i = 0; i < 256; i++) { lookup.put((byte) i); } lookup.rewind(); try (FileChannel lookupfile = FileChannel.open(Paths.get(lookupname), WRITE)) { for (int i = 0; i < 256; i++) { lookupfile.write(lookup); } } // TODO: Should be able to soften the auto-close without needing this catch (IOException ex) { throw new RuntimeException(ex); } } }
/** * Constructor, creates a new wsa file with the given name and data. * * @param name The name of this file. * @param bytechannel The data of the file. */ public WsaFileCNC(String name, ReadableByteChannel bytechannel) { super(name); // Read file header ByteBuffer headerbytes = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( WsaFileHeaderCNC.HEADER_SIZE); try { bytechannel.read(headerbytes); } catch (IOException e) { throw new RuntimeException(e); } headerbytes.rewind(); wsaheader = new WsaFileHeaderCNC(headerbytes); // Read offsets wsaoffsets = new int[numImages() + 2]; ByteBuffer offsetsbytes = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer( wsaoffsets.length * 4); try { bytechannel.read(offsetsbytes); } catch (IOException e) { throw new RuntimeException(e); } offsetsbytes.rewind(); for (int i = 0; i < wsaoffsets.length; i++) { wsaoffsets[i] = offsetsbytes.getInt(); } // Read internal palette ByteBuffer palettebytes = com.mikeduvall.redhorizon.util.ByteBufferFactory.createLittleEndianByteBuffer(PALETTE_SIZE); try { bytechannel.read(palettebytes); } catch (IOException e) { throw new RuntimeException(e); } palettebytes.rewind(); wsapalette = new WsaPalette(palettebytes); // Store seekable channel types if (bytechannel instanceof SeekableByteChannel) { this.bytechannel = (SeekableByteChannel) bytechannel; } // If the input channel isn't seekable, create a temp file that is seekable else { File tempanimdatafile = null; try { tempanimdatafile = File.createTempFile(name, null); tempanimdatafile.deleteOnExit(); FileChannel filechannel = FileChannel.open(Paths.get(tempanimdatafile.getAbsolutePath()), WRITE); filechannel.write(BufferUtility.readRemaining(bytechannel)); filechannel.position(0); this.bytechannel = filechannel; } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public void actionPerformed(ActionEvent e) { JFileChooser fc = new JFileChooser(); fc.setFileFilter(ImportAction.getFileFilter()); fc.setCurrentDirectory( new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT))); fc.setMultiSelectionEnabled(true); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { for (final File file : fc.getSelectedFiles()) { File dir = file; if (!dir.isDirectory()) { dir = dir.getParentFile(); } Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); try { final FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ); final ProgressHandle handle = ProgressHandleFactory.createHandle("Opening file " + file.getName()); handle.start(WORKUNITS); final long startTime = System.currentTimeMillis(); final long start = channel.size(); ParseMonitor monitor = new ParseMonitor() { @Override public void updateProgress() { try { int prog = (int) (WORKUNITS * (double) channel.position() / (double) start); handle.progress(prog); } catch (IOException ex) { } } @Override public void setState(String state) { updateProgress(); handle.progress(state); } }; final GraphParser parser; final OutlineTopComponent component = OutlineTopComponent.findInstance(); if (file.getName().endsWith(".xml")) { parser = new Parser(channel, monitor, null); } else if (file.getName().endsWith(".bgv")) { parser = new BinaryParser(channel, monitor, component.getDocument(), null); } else { parser = null; } RequestProcessor.getDefault() .post( new Runnable() { @Override public void run() { try { final GraphDocument document = parser.parse(); if (document != null) { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { component.requestActive(); component.getDocument().addGraphDocument(document); } }); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } handle.finish(); long stop = System.currentTimeMillis(); Logger.getLogger(getClass().getName()) .log( Level.INFO, "Loaded in " + file + " in " + ((stop - startTime) / 1000.0) + " seconds"); } }); } catch (FileNotFoundException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } } }
private static void bufferRead(String src, ByteBuffer buffer) throws IOException { try (FileChannel srcChannel = FileChannel.open(Paths.get(src), StandardOpenOption.READ)) { srcChannel.read(buffer); } }
/** Test exceptions specified by map method */ private static void testExceptions() throws Exception { // check exceptions when channel opened for read access try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) { testExceptions(fc); checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), NonWritableChannelException.class); checkException( fc, MapMode.READ_WRITE, -1L, fc.size(), NonWritableChannelException.class, IllegalArgumentException.class); checkException( fc, MapMode.READ_WRITE, 0L, -1L, NonWritableChannelException.class, IllegalArgumentException.class); checkException(fc, MapMode.PRIVATE, 0L, fc.size(), NonWritableChannelException.class); checkException( fc, MapMode.PRIVATE, -1L, fc.size(), NonWritableChannelException.class, IllegalArgumentException.class); checkException( fc, MapMode.PRIVATE, 0L, -1L, NonWritableChannelException.class, IllegalArgumentException.class); } // check exceptions when channel opened for write access try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) { testExceptions(fc); checkException(fc, MapMode.READ_ONLY, 0L, fc.size(), NonReadableChannelException.class); checkException( fc, MapMode.READ_ONLY, -1L, fc.size(), NonReadableChannelException.class, IllegalArgumentException.class); /* * implementation/spec mismatch, these tests disabled for now */ // checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), // NonWritableChannelException.class); // checkException(fc, MapMode.PRIVATE, 0L, fc.size(), // NonWritableChannelException.class); } // check exceptions when channel opened for read and write access try (FileChannel fc = FileChannel.open(blah.toPath(), READ, WRITE)) { testExceptions(fc); } }