/** * Maps blah file with a random offset and checks to see if read from the ByteBuffer gets the * right line number */ private static void testRead() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x = 0; x < 1000; x++) { try (FileInputStream fis = new FileInputStream(blah)) { FileChannel fc = fis.getChannel(); long offset = generator.nextInt(10000); long expectedResult = offset / CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE; MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100); for (int i = 0; i < 4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char) aByte); } int result = Integer.parseInt(sb.toString()); if (result != expectedResult) { err.println("I expected " + expectedResult); err.println("I got " + result); throw new Exception("Read test failed"); } } } }
/** * Maps blah file with a random offset and checks to see if data written out to the file can be * read back in */ private static void testWrite() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x = 0; x < 1000; x++) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { FileChannel fc = raf.getChannel(); long offset = generator.nextInt(1000); MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100); for (int i = 0; i < 4; i++) { b.put(i, (byte) ('0' + i)); } for (int i = 0; i < 4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char) aByte); } if (!sb.toString().equals("0123")) throw new Exception("Write test failed"); } } }
public static void createTestFiles() { try { System.out.println("Creating test files ... "); Random rand = new Random(); String rootname = "f-"; long[] sizes = {0, 1, 50000000}; File testdir = new File(dirname); FileUtil.mkdirs(testdir); for (int i = 0; i < sizes.length; i++) { long size = sizes[i]; File file = new File(testdir, rootname + String.valueOf(size)); System.out.println(file.getName() + "..."); FileChannel fc = new RandomAccessFile(file, "rw").getChannel(); long position = 0; while (position < size) { long remaining = size - position; if (remaining > 1024000) remaining = 1024000; byte[] buffer = new byte[new Long(remaining).intValue()]; rand.nextBytes(buffer); ByteBuffer bb = ByteBuffer.wrap(buffer); position += fc.write(bb); } fc.close(); } System.out.println("DONE\n"); } catch (Exception e) { Debug.printStackTrace(e); } }
private static void testExceptions(FileChannel fc) throws IOException { checkException(fc, null, 0L, fc.size(), NullPointerException.class); checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), IllegalArgumentException.class); checkException( fc, null, -1L, fc.size(), IllegalArgumentException.class, NullPointerException.class); checkException(fc, MapMode.READ_ONLY, 0L, -1L, IllegalArgumentException.class); checkException(fc, null, 0L, -1L, IllegalArgumentException.class, NullPointerException.class); checkException( fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L, IllegalArgumentException.class); checkException( fc, null, 0L, Integer.MAX_VALUE + 1L, IllegalArgumentException.class, NullPointerException.class); checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L, IllegalArgumentException.class); checkException( fc, null, Long.MAX_VALUE, 1L, IllegalArgumentException.class, NullPointerException.class); }
public static void main(String[] arguments) { try { // read byte data into a byte buffer String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); System.out.println("Original byte data:"); for (int i = 0; source.remaining() > 0; i++) { System.out.print(source.get() + " "); } // convert byte data into character data source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) { System.out.print(destination.get()); } System.out.println(); } catch (FileNotFoundException fne) { System.out.println(fne.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } }
public static void main(String args[]) { try { aServer asr = new aServer(); // file channel. FileInputStream is = new FileInputStream(""); is.read(); FileChannel cha = is.getChannel(); ByteBuffer bf = ByteBuffer.allocate(1024); bf.flip(); cha.read(bf); // Path Paths Path pth = Paths.get("", ""); // Files some static operation. Files.newByteChannel(pth); Files.copy(pth, pth); // file attribute, other different class for dos and posix system. BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class); bas.size(); } catch (Exception e) { System.err.println(e); } System.out.println("hello "); }
/* * Creates a file at filename if file doesn't exist. Writes * value to that file using FileChannel. */ public static void writeToFile(File filename, String value, String hideCommand) throws IOException, InterruptedException { if (!hideCommand.trim().equals("")) { // unhideFile(filename); } if (!filename.exists()) { filename.createNewFile(); } byte[] bytes = value.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); for (int i = 0; i < bytes.length; i++) { buffer.put(bytes[i]); } buffer.rewind(); try { fileWriter = new FileOutputStream(filename).getChannel(); fileWriter.write(buffer); } finally { fileWriter.close(); } if (!hideCommand.trim().equals("")) { // hideFile(filename); } }
public static void main(String args[]) throws Exception { String inputFile = "samplein.txt"; String outputFile = "sampleout.txt"; RandomAccessFile inf = new RandomAccessFile(inputFile, "r"); RandomAccessFile outf = new RandomAccessFile(outputFile, "rw"); long inputLength = new File(inputFile).length(); FileChannel inc = inf.getChannel(); FileChannel outc = outf.getChannel(); MappedByteBuffer inputData = inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength); Charset latin1 = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = latin1.newDecoder(); CharsetEncoder encoder = latin1.newEncoder(); CharBuffer cb = decoder.decode(inputData); // Process char data here ByteBuffer outputData = encoder.encode(cb); outc.write(outputData); inf.close(); outf.close(); }
private boolean tagExists(FileChannel fc) throws IOException { ByteBuffer b = ByteBuffer.allocate(3); fc.position(0); fc.read(b); String tagString = new String(b.array()); return tagString.equals("ID3"); }
public static void runTests() { try { // SHA1 sha1Jmule = new SHA1(); MessageDigest sha1Sun = MessageDigest.getInstance("SHA-1"); SHA1 sha1Gudy = new SHA1(); // SHA1Az shaGudyResume = new SHA1Az(); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); File dir = new File(dirname); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { FileChannel fc = new RandomAccessFile(files[i], "r").getChannel(); System.out.println("Testing " + files[i].getName() + " ..."); while (fc.position() < fc.size()) { fc.read(buffer); buffer.flip(); byte[] raw = new byte[buffer.limit()]; System.arraycopy(buffer.array(), 0, raw, 0, raw.length); sha1Gudy.update(buffer); sha1Gudy.saveState(); ByteBuffer bb = ByteBuffer.wrap(new byte[56081]); sha1Gudy.digest(bb); sha1Gudy.restoreState(); sha1Sun.update(raw); buffer.clear(); } byte[] sun = sha1Sun.digest(); sha1Sun.reset(); byte[] gudy = sha1Gudy.digest(); sha1Gudy.reset(); if (Arrays.equals(sun, gudy)) { System.out.println(" SHA1-Gudy: OK"); } else { System.out.println(" SHA1-Gudy: FAILED"); } buffer.clear(); fc.close(); System.out.println(); } } catch (Throwable e) { Debug.printStackTrace(e); } }
/** * Creates and initializes an SPV block store. Will create the given file if it's missing. This * operation will block on disk. */ public SPVBlockStore(NetworkParameters params, File file) throws BlockStoreException { checkNotNull(file); this.params = checkNotNull(params); try { this.numHeaders = DEFAULT_NUM_HEADERS; boolean exists = file.exists(); // Set up the backing file. randomAccessFile = new RandomAccessFile(file, "rw"); long fileSize = getFileSize(); if (!exists) { log.info("Creating new SPV block chain file " + file); randomAccessFile.setLength(fileSize); } else if (randomAccessFile.length() != fileSize) { throw new BlockStoreException( "File size on disk does not match expected size: " + randomAccessFile.length() + " vs " + fileSize); } FileChannel channel = randomAccessFile.getChannel(); fileLock = channel.tryLock(); if (fileLock == null) throw new BlockStoreException("Store file is already locked by another process"); // Map it into memory read/write. The kernel will take care of flushing writes to disk at the // most // efficient times, which may mean that until the map is deallocated the data on disk is // randomly // inconsistent. However the only process accessing it is us, via this mapping, so our own // view will // always be correct. Once we establish the mmap the underlying file and channel can go away. // Note that // the details of mmapping vary between platforms. buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize); // Check or initialize the header bytes to ensure we don't try to open some random file. byte[] header; if (exists) { header = new byte[4]; buffer.get(header); if (!new String(header, "US-ASCII").equals(HEADER_MAGIC)) throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC); } else { initNewStore(params); } } catch (Exception e) { try { if (randomAccessFile != null) randomAccessFile.close(); } catch (IOException e2) { throw new BlockStoreException(e2); } throw new BlockStoreException(e); } }
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("arguments: sourcefile destfile"); System.exit(1); } FileChannel in = new FileInputStream(args[0]).getChannel(), out = new FileOutputStream(args[1]).getChannel(); in.transferTo(0, in.size(), out); // Or: // out.transferFrom(in, 0, in.size()); }
private static void testHighOffset() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x = 0; x < 1000; x++) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { FileChannel fc = raf.getChannel(); long offset = 66000; MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100); } } }
/** * Read the contents of a text file using a memory-mapped byte buffer. * * <p>A MappedByteBuffer, is simply a special ByteBuffer. MappedByteBuffer maps a region of a file * directly in memory. Typically, that region comprises the entire file, although it could map a * portion. You must, therefore, specify what part of the file to map. Moreover, as with the other * Buffer objects, no constructor exists; you must ask the java.nio.channels.FileChannel for its * map() method to get a MappedByteBuffer. * * <p>Direct buffers allocate their data directly in the runtime environment memory, bypassing the * JVM|OS boundary, usually doubling file copy speed. However, they generally cost more to * allocate. */ private static String fastStreamCopy(String filename) { String s = ""; FileChannel fc = null; try { fc = new FileInputStream(filename).getChannel(); // int length = (int)fc.size(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); // CharBuffer charBuffer = // Charset.forName("ISO-8859-1").newDecoder().decode(byteBuffer); // ByteBuffer byteBuffer = ByteBuffer.allocate(length); // ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length); // CharBuffer charBuffer = byteBuffer.asCharBuffer(); // CharBuffer charBuffer = // ByteBuffer.allocateDirect(length).asCharBuffer(); /* * int size = charBuffer.length(); if (size > 0) { StringBuffer sb = * new StringBuffer(size); for (int count=0; count<size; count++) * sb.append(charBuffer.get()); s = sb.toString(); } * * if (length > 0) { StringBuffer sb = new StringBuffer(length); for * (int count=0; count<length; count++) { * sb.append(byteBuffer.get()); } s = sb.toString(); } */ int size = byteBuffer.capacity(); if (size > 0) { // Retrieve all bytes in the buffer byteBuffer.clear(); byte[] bytes = new byte[size]; byteBuffer.get(bytes, 0, bytes.length); s = new String(bytes); } fc.close(); } catch (FileNotFoundException fnfx) { System.err.println("File not found: " + fnfx); } catch (IOException iox) { System.err.println("I/O problems: " + iox); } finally { if (fc != null) { try { fc.close(); } catch (IOException ignore) { // ignore } } } return s; }
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 write(FileOutputStream fos) throws IOException { FileChannel chan = fos.getChannel(); // Create ByteBuffer for header in case the start of our // ByteBuffer isn't actually memory-mapped ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize()); hdr.order(ByteOrder.LITTLE_ENDIAN); header.write(hdr); hdr.rewind(); chan.write(hdr); buf.position(Header.writtenSize()); chan.write(buf); chan.force(true); chan.close(); }
/** * Checks that FileChannel map throws one of the expected exceptions when invoked with the given * inputs. */ private static void checkException( FileChannel fc, MapMode mode, long position, long size, Class<?>... expected) throws IOException { Exception exc = null; try { fc.map(mode, position, size); } catch (Exception actual) { exc = actual; } if (exc != null) { for (Class<?> clazz : expected) { if (clazz.isInstance(exc)) { return; } } } System.err.println("Expected one of"); for (Class<?> clazz : expected) { System.out.println(clazz); } if (exc == null) { throw new RuntimeException("No expection thrown"); } else { throw new RuntimeException("Unexpected exception thrown", exc); } }
public static void main(String[] args) throws Exception { fc = new RandomAccessFile("test.dat", "rw").getChannel(); MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH); for (int i = 0; i < LENGTH; i++) out.put((byte) 'x'); new LockAndModify(out, 0, 0 + LENGTH / 3); new LockAndModify(out, LENGTH / 2, LENGTH / 2 + LENGTH / 4); }
public static void main(String args[]) throws IOException { if (args.length != 1) { System.err.println("Usage: java LockingExample <input file>"); System.exit(0); } FileLock sharedLock = null; FileLock exclusiveLock = null; try { RandomAccessFile raf = new RandomAccessFile(args[0], "rw"); // get the channel for the file FileChannel channel = raf.getChannel(); System.out.println("trying to acquire lock ..."); // this locks the first half of the file - exclusive exclusiveLock = channel.lock(0, raf.length() / 2, SHARED); System.out.println("lock acquired ..."); /** Now modify the data . . . */ try { // sleep for 10 seconds Thread.sleep(10000); } catch (InterruptedException ie) { } // release the lock exclusiveLock.release(); System.out.println("lock released ..."); // this locks the second half of the file - shared sharedLock = channel.lock(raf.length() / 2 + 1, raf.length(), SHARED); /** Now read the data . . . */ // release the lock sharedLock.release(); } catch (java.io.IOException ioe) { System.err.println(ioe); } finally { if (exclusiveLock != null) exclusiveLock.release(); if (sharedLock != null) sharedLock.release(); } }
public static void main(String args[]) throws Exception { FileInputStream fin = new FileInputStream("readandshow.txt"); FileChannel fc = fin.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); fc.read(buffer); buffer.flip(); int i = 0; while (buffer.remaining() > 0) { byte b = buffer.get(); System.out.println("Character " + i + ": " + ((char) b)); i++; } fin.close(); }
public static void main(String[] args) throws Exception { File blah = File.createTempFile("blah", null); blah.deleteOnExit(); ByteBuffer[] dstBuffers = new ByteBuffer[10]; for (int i = 0; i < 10; i++) { dstBuffers[i] = ByteBuffer.allocateDirect(10); dstBuffers[i].position(10); } FileInputStream fis = new FileInputStream(blah); FileChannel fc = fis.getChannel(); // No space left in buffers, this should return 0 long bytesRead = fc.read(dstBuffers); if (bytesRead != 0) throw new RuntimeException("Nonzero return from read"); fc.close(); fis.close(); }
/* * TODO: Finish method. */ public static String readBytesFromFile(File filename) throws IOException, OverlappingFileLockException { if (!filename.exists()) { return null; } try { ByteBuffer buffer = ByteBuffer.allocate(((int) filename.getTotalSpace() * 4)); fileReader = new FileInputStream(filename).getChannel(); FileLock lock = fileReader.tryLock(); if (lock != null) { fileReader.read(buffer); } else { throw new OverlappingFileLockException(); } } finally { fileWriter.close(); } return ""; }
/** * Decode file charset. * * @param f File to process. * @return File charset. * @throws IOException in case of error. */ public static Charset decode(File f) throws IOException { SortedMap<String, Charset> charsets = Charset.availableCharsets(); String[] firstCharsets = { Charset.defaultCharset().name(), "US-ASCII", "UTF-8", "UTF-16BE", "UTF-16LE" }; Collection<Charset> orderedCharsets = U.newLinkedHashSet(charsets.size()); for (String c : firstCharsets) if (charsets.containsKey(c)) orderedCharsets.add(charsets.get(c)); orderedCharsets.addAll(charsets.values()); try (RandomAccessFile raf = new RandomAccessFile(f, "r")) { FileChannel ch = raf.getChannel(); ByteBuffer buf = ByteBuffer.allocate(4096); ch.read(buf); buf.flip(); for (Charset charset : orderedCharsets) { CharsetDecoder decoder = charset.newDecoder(); decoder.reset(); try { decoder.decode(buf); return charset; } catch (CharacterCodingException ignored) { } } } return Charset.defaultCharset(); }
public void run() { try { // Exclusive lock with no overlap: FileLock fl = fc.lock(start, end, false); System.out.println("Locked: " + start + " to " + end); // Perform modification: while (buff.position() < buff.limit() - 1) buff.put((byte) (buff.get() + 1)); fl.release(); System.out.println("Released: " + start + " to " + end); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Closes open files and resources associated with the open DDSImage. No other methods may be * called on this object once this is called. */ public void close() { try { if (chan != null) { chan.close(); chan = null; } if (fis != null) { fis.close(); fis = null; } buf = null; } catch (IOException e) { e.printStackTrace(); } }
public static void copyFileNIO(String filenameIn, String filenameOut, long kbchunks) throws IOException { FileInputStream in = new FileInputStream(filenameIn); FileChannel inChannel = in.getChannel(); FileOutputStream out = new FileOutputStream(filenameOut); FileChannel outChannel = out.getChannel(); long size = inChannel.size(); // outChannel.position(size-2); // outChannel.write(ByteBuffer.allocate(1)); // outChannel.position(0); if (debug) System.out.println( "read " + filenameIn + " len = " + size + " out starts at=" + outChannel.position()); long start = System.currentTimeMillis(); long done = 0; while (done < size) { long need = Math.min(kbchunks * 1000, size - done); done += inChannel.transferTo(done, need, outChannel); } outChannel.close(); inChannel.close(); double took = .001 * (System.currentTimeMillis() - start); if (debug) System.out.println(" write file= " + filenameOut + " len = " + size); double rate = size / took / (1000 * 1000); System.out.println( " copyFileNIO(" + kbchunks + " kb chunk) took = " + took + " sec; rate = " + rate + "Mb/sec"); }
public static void main(String[] args) throws Exception { String fromFileName = args[0]; String toFileName = args[1]; FileChannel in = new FileInputStream(fromFileName).getChannel(); FileChannel out = new FileOutputStream(toFileName).getChannel(); ByteBuffer buff = ByteBuffer.allocate(32 * 1024); while (in.read(buff) > 0) { buff.flip(); out.write(buff); buff.clear(); } in.close(); out.close(); }
/** * copies file "in" to file "out" * * @param in * @param out * @throws java.io.IOException */ public static boolean copy(File in, File out) { Boolean returnVal = true; try { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { returnVal = false; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } catch (Exception ea) { returnVal = false; } return returnVal; }
/* * Perform a FileChannel.TransferTo on the socket channel. * <P> * We have to copy the data into an intermediary app ByteBuffer * first, then send it through the SSLEngine. * <P> * We return the number of bytes actually read out of the * filechannel. However, the data may actually be stuck * in the fileChannelBB or the outNetBB. The caller * is responsible for making sure to call dataFlush() * before shutting down. */ long transferTo(FileChannel fc, long pos, long len) throws IOException { if (!initialHSComplete) { throw new IllegalStateException(); } if (fileChannelBB == null) { fileChannelBB = ByteBuffer.allocate(appBBSize); fileChannelBB.limit(0); } fileChannelBB.compact(); int fileRead = fc.read(fileChannelBB); fileChannelBB.flip(); /* * We ignore the return value here, we return the * number of bytes actually consumed from the the file. * We'll flush the output buffer before we start shutting down. */ doWrite(fileChannelBB); return fileRead; }
/* * Method used to move a file from sourceFile to destFile */ public static void moveFile(File sourceFile, File destFile) throws IOException { FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); long count = 0; long size = source.size(); source.transferTo(count, size, destination); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }