/** * 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 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(); }
/** * 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); }
/** * 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); } }
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(); } }
private void readFromFile(File file) throws IOException { fis = new FileInputStream(file); chan = fis.getChannel(); ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, (int) file.length()); readFromBuffer(buf); }
/** Tests zero size file mapping */ private static void testZero() throws Exception { try (FileInputStream fis = new FileInputStream(blah)) { FileChannel fc = fis.getChannel(); MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0); } }