Package java.nio.channels

Examples of java.nio.channels.FileChannel$MapMode


        preFileAppend(file, append);
        final FileOutputStream dst = new FileOutputStream(file, append);
        final String fp = file.getAbsolutePath();
        final ReadWriteLock filelock = accquireLock(fp, locks);
        final FileChannel fileCh = dst.getChannel();
        final long startPos = file.length();
        try {
            NIOUtils.transferFullyFrom(inChannel, 0, len, fileCh); // REVIEWME really an atomic operation?
        } finally {
            IOUtils.closeQuietly(fileCh, dst);
View Full Code Here


     * Create a temp file
     */

    final File temp = File.createTempFile(this.file.getName(), null);

    final FileChannel tempChannel = new RandomAccessFile(temp, "rw")
        .getChannel();

    /*
     * Copy entire edits tree, including the root file, to temp file
     */
    for (final RegionSegment<PartialLoader> segment : this.edits) {
      final PartialLoader loader = new ConstrainedPartialLoader(segment
          .getData(), segment);

      loader.transferTo(tempChannel);
    }
    this.channel.close();
    tempChannel.close();

    System.gc();

    /*
     * We're done with the original file. All changes are now in the temp file
View Full Code Here

    if (dst.createNewFile() == false) {
      throw new FileNotFoundException("Unable to create dst file ["
          + dst.getName() + "]");
    }

    final FileChannel srcC = new RandomAccessFile(src, "r").getChannel();

    final FileChannel dstC = new RandomAccessFile(dst, "rw").getChannel();

    srcC.transferTo(0, srcC.size(), dstC);

    srcC.close();
    dstC.close();
  }
View Full Code Here

    out.close();

  }

  public static long gzipUncompressedSize(File src) throws IOException {
    FileChannel in = new RandomAccessFile(src, "r").getChannel();

    ByteBuffer b = ByteBuffer.allocate(128);
    b.order(ByteOrder.LITTLE_ENDIAN);

    in.read(b, in.size() - b.remaining());

    final long size = b.getInt(b.limit() - 4);

    in.close();

    return size;
  }
View Full Code Here

    return size;
  }

  public static long gzipUncompressedCRC32(File src) throws IOException {
    FileChannel in = new RandomAccessFile(src, "r").getChannel();

    ByteBuffer b = ByteBuffer.allocate(128);
    b.order(ByteOrder.LITTLE_ENDIAN);

    in.read(b, in.size() - b.remaining());

    final long size = b.getInt(b.limit() - 8);

    in.close();

    return (size < 0 ? ((long) Integer.MAX_VALUE )* 2L + 1 + size : size);
  }
View Full Code Here

        final StopWatch sw = new StopWatch();
        FileInputStream src = null;
        final long nbytes;
        try {
            src = new FileInputStream(file);
            FileChannel fc = src.getChannel();

            String fileName = file.getName();
            IOUtils.writeString(fileName, dos);
            IOUtils.writeString(writeDirPath, dos);
            long xferBytes = (count == -1L) ? fc.size() : count;
            dos.writeLong(xferBytes);
            dos.writeBoolean(append); // append=false
            dos.writeBoolean(sync);
            if(handler == null) {
                dos.writeBoolean(false);
            } else {
                dos.writeBoolean(true);
                handler.writeAdditionalHeader(dos);
            }

            // send file using zero-copy send
            nbytes = fc.transferTo(fromPos, xferBytes, channel);
            if(LOG.isDebugEnabled()) {
                LOG.debug("Sent a file '" + file.getAbsolutePath() + "' of " + nbytes
                        + " bytes to " + dstSockAddr.toString() + " in " + sw.toString());
            }
View Full Code Here

    private int hslots;

    public ReadOnlyCDB(String filename) throws IOException {
        file = new RandomAccessFile(filename, "r");
        FileChannel channel = file.getChannel();
        map = channel.map(MapMode.READ_ONLY, 0, file.length());
        map.order(ByteOrder.LITTLE_ENDIAN);
    }
View Full Code Here

   * @param f            The file to open that contains binary logs.
   * @throws IOException
   */
  public void read(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc=fis.getChannel();
    buffer=fc.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
    int o=0;
    do{
      int l=buffer.getInt(o);
      if(l<=STRING_OFFSET){
        throw new IOException("Corrupted file : invalid packet length");
View Full Code Here

   * @param f                The file to write to.
   * @throws IOException
   */
  public void save(File f) throws IOException {
    FileOutputStream fos=new FileOutputStream(f);
    FileChannel fc=fos.getChannel();
    buffer.flip();
    fc.write(buffer);
    buffer.limit(buffer.capacity());
    fc.close();
    fos.flush();
    fos.close();
  }
View Full Code Here

      if (src.length()==0) {
        dest.createNewFile();
        return;
      }
      // transfering >0 bytes
      FileChannel fc = new FileInputStream(src).getChannel();
      FileChannel dstChannel = new FileOutputStream(dest).getChannel();
      long transfered = 0;
      long totalLength = src.length();
      while (transfered < totalLength) {
        long num = fc.transferTo(transfered,totalLength-transfered,dstChannel);
        if (num==0) throw new IOException("Error while copying");
        transfered += num;
      }
      dstChannel.close();
      fc.close();
    } catch (IOException e) {
      if (os.equals("Unix")) {
       
        _logger.fine("Trying to use cp to copy file...");
View Full Code Here

TOP

Related Classes of java.nio.channels.FileChannel$MapMode

Copyright © 2018 www.massapicom. 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.