Package java.nio.channels

Examples of java.nio.channels.FileChannel.position()


                try {
                    FileChannel channel = randomAccessFile.getChannel();
                    try {
                        long length = randomAccessFile.length();

                        channel = channel.position(length - bytesInLineBreak.length);
                        channel.read(ByteBuffer.wrap(bytesFromFile));
                    } finally {
                        channel.close();
                    }
                } finally {
View Full Code Here


            byteBuffer = ByteBuffer.wrap(bufData);
            fileInputStream = new FileInputStream(filepath);
            fileChannel = fileInputStream.getChannel();
            int counter = 0;
            for (int i = 0; i < this.z; i++) {
                fileChannel.position(i * this.x * this.y / Byte.SIZE);
                byteBuffer.position(0);
                // Save position
                int pos = byteBuffer.position();
                // Set position to zero
                byteBuffer.position(0);
View Full Code Here

        if (file.exists() && file.canRead()) {
          inputStream = new FileInputStream(file);
          FileChannel channel = inputStream.getChannel();
          //if offset is mentioned move the pointer to that point
          if (offset != -1)
            channel.position(offset);
          byte[] buf = new byte[(len == -1 || len > PACKET_SZ) ? PACKET_SZ : len];
          Checksum checksum = null;
          if (useChecksum)
            checksum = new Adler32();
          ByteBuffer bb = ByteBuffer.wrap(buf);
View Full Code Here

       
        ByteBuffer buffer = null;
        if (src instanceof FileChannel) {
            FileChannel fileSrc = (FileChannel) src;
            long size = fileSrc.size();
            long filePosition = fileSrc.position();
            count = Math.min(count, size - filePosition);
            buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
            fileSrc.position(filePosition + count);
        } else {
            buffer = ByteBuffer.allocateDirect((int) count);
View Full Code Here

            FileChannel fileSrc = (FileChannel) src;
            long size = fileSrc.size();
            long filePosition = fileSrc.position();
            count = Math.min(count, size - filePosition);
            buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
            fileSrc.position(filePosition + count);
        } else {
            buffer = ByteBuffer.allocateDirect((int) count);
            src.read(buffer);
            buffer.flip();
        }
View Full Code Here

      crp = new ContentRangeParser (cr, logger);
  if (!crp.isValid ())
      throw new IllegalArgumentException ("bad range: " + cr);
  RandomAccessFile raf = new RandomAccessFile (fileName, "rw");
  FileChannel fc = raf.getChannel ();
  fc.position (crp.getStart ());
  fw = new FileWriter (fc);
    }
   
    private class FileWriter implements WritableByteChannel {
  private FileChannel fc;
View Full Code Here

      FileChannel fc = fis.getChannel();
      long size = fc.size();
      long pos = size - (size % chunkSizeToRead);
     
      while (pos >= 0) {
        fc.position(pos);
 
        int readLen = (int) Math.min(size - pos, chunkSizeToRead);
        IOUtils.readFully(fis, buf, 0, readLen);
        for (int i = readLen - 1; i >= 0; i--) {
          if (buf[i] != FSEditLogOpCodes.OP_INVALID.getOpCode()) {
View Full Code Here

    public void testSimpleExpandTruncateSize() throws Exception {
        String f = getBaseDir() + "/fs/test.data";
        FileUtils.createDirectories(getBaseDir() + "/fs");
        FileChannel c = FileUtils.open(f, "rw");
        c.position(4000);
        c.write(ByteBuffer.wrap(new byte[1]));
        FileLock lock = c.tryLock();
        c.truncate(0);
        if (lock != null) {
            lock.release();
View Full Code Here

        FileUtils.createFile(fileName);
        // Profiler prof = new Profiler();
        // prof.interval = 1;
        // prof.startCollecting();
        FileChannel c = FileUtils.open(fileName, "rw");
        c.position(0);
        // long t = System.currentTimeMillis();
        byte[] array = new byte[100];
        ByteBuffer buff = ByteBuffer.wrap(array);
        for (int i = 0; i < 100000; i++) {
            array[0] = (byte) i;
View Full Code Here

        byte[] buffer = new byte[10000];
        Random random = new Random(1);
        random.nextBytes(buffer);
        channel.write(ByteBuffer.wrap(buffer));
        assertEquals(10000, channel.size());
        channel.position(20000);
        assertEquals(20000, channel.position());
        assertEquals(-1, channel.read(ByteBuffer.wrap(buffer, 0, 1)));
        String path = fsBase + "/test";
        assertEquals("test", FileUtils.getName(path));
        can = FilePath.get(fsBase).toRealPath().toString();
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.