/** * 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"); } } } }
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 "); }
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[]) 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(); }
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[] argv) { if (argv.length != 3) { usage(); } String tempFile = argv[0]; String testFile = argv[1]; int fileSize = Integer.valueOf(argv[2]).intValue(); int exitcode = 0; int numRead; int numThisBuf; int numWritten; if ((fileSize <= 0) || (fileSize % 4096 != 0)) { System.out.println("Error: size is not a multiple of 4096!!!!!!"); System.out.println(); usage(); } try { int bufSize = 4096; byte[] inBytes = new byte[bufSize]; byte[] outBytes = new byte[bufSize]; Random ioRandom = new Random(2006); ioRandom.nextBytes(outBytes); ByteBuffer inBuf = ByteBuffer.allocate(bufSize); ByteBuffer outBuf = ByteBuffer.wrap(outBytes); // // Loop forever // while (true) { // // Write the temporary file // FileOutputStream fos = new FileOutputStream(tempFile); FileChannel foc = fos.getChannel(); numWritten = 0; while (numWritten < fileSize) { outBuf.clear(); // sets limit to capacity & position to zero while (outBuf.hasRemaining()) { numWritten += foc.write(outBuf); } } // // Move to permanent location // FileChannel srcChannel = new FileInputStream(tempFile).getChannel(); FileChannel dstChannel = new FileOutputStream(testFile).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); boolean success = (new File(tempFile)).delete(); if (!success) { System.out.println("Warning: unable to delete temporary file"); } // // Read and compare // FileInputStream fis = new FileInputStream(testFile); FileChannel fic = fis.getChannel(); for (numRead = 0, numThisBuf = 0; numRead < fileSize; numThisBuf = 0) { inBuf.rewind(); // Set the buffer position to 0 numThisBuf = fic.read(inBuf); while (numThisBuf < bufSize) { numThisBuf += fic.read(inBuf); } numRead += bufSize; inBuf.rewind(); // Set the buffer position to 0 inBuf.get(inBytes); boolean same = Arrays.equals(inBytes, outBytes); if (same = false) { System.out.println("Data read does not equal data written at " + numRead + " bytes"); } } } } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(System.err); exitcode = 1; // break; } catch (SecurityException se) { se.printStackTrace(System.err); exitcode = 1; // break; } catch (Throwable t) { t.printStackTrace(System.err); exitcode = 1; } System.exit(exitcode); }
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); } }