Package java.nio.channels

Examples of java.nio.channels.FileChannel$MapMode


            FileLock lock = null;
            boolean writable;

            try {
                fos = new FileOutputStream(file, true);
                FileChannel channel = fos.getChannel();
                lock = channel.tryLock();
            } catch (IOException e) {
                log.warn("Error while attempting to lock the file: " + file.getName(), e);
                writable = false;
            } finally {
                if (lock != null) {
View Full Code Here


    }

    public static void handleResponse(final ReadRequestMessage request, final ProtocolEncoderOutput out, final ConcurrentMap<String, FileChannel> fdCacheMap) {
        final String filePath = request.filePath;

        FileChannel fileChannel = fdCacheMap.get(filePath);
        if(fileChannel == null) {
            File file = new File(filePath);
            if(!file.exists()) {
                throw new IllegalStateException("file not exists: " + filePath);
            }
View Full Code Here

        } catch (IOException e) {
            LOG.error(e);
            throw e;
        }

        FileChannel fileChannel = fdCacheMap.get(filePath);
        if(fileChannel == null) {
            if(!dataFile.exists()) {
                throw new IllegalStateException("file not exists: " + filePath);
            }
            final RandomAccessFile raf;
            try {
                raf = new RandomAccessFile(dataFile, "r");
            } catch (FileNotFoundException e) {
                throw new IllegalStateException(e);
            }
            fileChannel = raf.getChannel();
            fdCacheMap.put(filePath, fileChannel);
        }

        for(int i = 0; i < size; i++) {
            final long offset = offsets[i];
            // get data length
            final ByteBuffer tmpBuf = ByteBuffer.allocate(4);
            try {
                fileChannel.read(tmpBuf, offset);
            } catch (IOException e) {
                LOG.error(e);
                throw e;
            }
            tmpBuf.flip();
View Full Code Here

      throw new RuntimeException(e);
    }
  }

  private static ByteBuffer readFile(final File file) throws IOException {
    final FileChannel channel = new FileInputStream(file).getChannel();

    final long bytesTotal = channel.size();
    final ByteBuffer buffer = ByteBuffer.allocateDirect((int)bytesTotal);

    long bytesRead = 0;
    do {
      bytesRead += channel.read(buffer);
    } while ( bytesRead < bytesTotal );
    buffer.flip();

    channel.close();

    return buffer;
  }
View Full Code Here

  }
 

  private MappedByteBuffer createMappedBuffer( long file_offset, int length ) throws IOException {
    if ( channel == null ) {
      FileChannel fc = new RandomAccessFile( file, "rw" ).getChannel();
      MappedByteBuffer mbb = fc.map( FileChannel.MapMode.READ_WRITE, file_offset, length );
      if ( access_mode == MODE_READ_ONLY ) fc.close();
      else channel = fc;
      return mbb;
    }
   
    return channel.map( FileChannel.MapMode.READ_WRITE, file_offset, length );
View Full Code Here

    if (raf == null){
     
      throw new FMFileManagerException( "read: raf is null" );
    }
   
    FileChannel fc = raf.getChannel();
       
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }

    AEThread2.setDebug( owner );
   
    try{
      if(USE_MMAP)
      {
        long remainingInFile = fc.size()-offset;
        long remainingInTargetBuffer = buffer.remaining(DirectByteBuffer.SS_FILE);
        MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, Math.min(remainingInFile,remainingInTargetBuffer));
        buffer.put(DirectByteBuffer.SS_FILE, buf);
      } else {
        fc.position(offset);
        while (fc.position() < fc.size() && buffer.hasRemaining(DirectByteBuffer.SS_FILE))
          buffer.read(DirectByteBuffer.SS_FILE,fc);       
      }

     
     
View Full Code Here

    if ( raf == null ){
     
      throw new FMFileManagerException( "read: raf is null" );
    }
   
    FileChannel fc = raf.getChannel();
       
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }
   
    AEThread2.setDebug( owner );
   
    int[]  original_positions = new int[buffers.length];
   
    long read_start = SystemTime.getHighPrecisionCounter();
   
    try{
      if(USE_MMAP)
      {
       
        long size = 0;
        for(int i=0;i<buffers.length;i++)
        {
          size += buffers[i].remaining(DirectByteBuffer.SS_FILE);
          original_positions[i] = buffers[i].position(DirectByteBuffer.SS_FILE);
        }
         
        size = Math.min(size, fc.size()-offset);
        MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, size);
        for(DirectByteBuffer b : buffers)
        {
          buf.limit(buf.position()+b.remaining(DirectByteBuffer.SS_FILE));
          b.put(DirectByteBuffer.SS_FILE, buf);
        }
         
       
      } else {
       
        fc.position(offset);
        ByteBuffer[]  bbs = new ByteBuffer[buffers.length];
       
        ByteBuffer  last_bb  = null;
        for (int i=0;i<bbs.length;i++){
          ByteBuffer bb = bbs[i] = buffers[i].getBuffer(DirectByteBuffer.SS_FILE);
          int  pos = original_positions[i] = bb.position();
          if ( pos != bb.limit()){
            last_bb  = bbs[i];
          }
        }
       
        if ( last_bb != null ){
          int    loop      = 0;
         
            // we sometimes read off the end of the file (when rechecking) so
            // bail out if we've completed the read or got to file end
            // a "better" fix would be to prevent the over-read in the first
            // place, but hey, we're just about to release and there may be other
            // instances of this...

          while ( fc.position() < fc.size() && last_bb.hasRemaining()){
            long  read = fc.read( bbs );
            if ( read > 0 ){
              loop  = 0;
            }else{
              loop++;
              if ( loop == READ_RETRY_LIMIT ){
View Full Code Here

  {
    if ( raf == null){
      throw( new FMFileManagerException( "write fails: raf is null" ));
    }
   
    FileChannel fc = raf.getChannel();
   
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }

    AEThread2.setDebug( owner );
   
    int[]  original_positions = new int[buffers.length];

    try{
     
      if(USE_MMAP) {
        long size = 0;
        for(int i=0;i<buffers.length;i++)
        {
          size += buffers[i].remaining(DirectByteBuffer.SS_FILE);
          original_positions[i] = buffers[i].position(DirectByteBuffer.SS_FILE);
        }
       
        if(position+size > fc.size())
        {
          fc.position(position+size-1);
          fc.write(ByteBuffer.allocate(1));
          fc.force(true);
        }
         
        MappedByteBuffer buf = fc.map(MapMode.READ_WRITE, position, size);
        for(DirectByteBuffer b : buffers)
          buf.put(b.getBuffer(DirectByteBuffer.SS_FILE));
        buf.force();
      } else {
        long  expected_write   = 0;
        long  actual_write  = 0;
        boolean  partial_write  = false;
       
        if ( DEBUG ){
          for (int i=0;i<buffers.length;i++){
            expected_write += buffers[i].limit(DirectByteBuffer.SS_FILE) - buffers[i].position(DirectByteBuffer.SS_FILE);
          }
        }
       
        fc.position( position );
        ByteBuffer[]  bbs = new ByteBuffer[buffers.length];


        ByteBuffer  last_bb  = null;
        for (int i=0;i<bbs.length;i++){
          ByteBuffer bb = bbs[i] = buffers[i].getBuffer(DirectByteBuffer.SS_FILE);
          int  pos = original_positions[i] = bb.position();
          if ( pos != bb.limit()){
            last_bb  = bbs[i];
          }
        }
       
        if ( last_bb != null ){
          int loop = 0;
         
          while( last_bb.position() != last_bb.limit()){
            long written = fc.write( bbs );
            actual_write += written;
           
            if ( written > 0 ){
              loop  = 0;
              if ( DEBUG ){
View Full Code Here

                RandomAccessFile raf = null;
                PDFFile pdffile;
                try {
                   
                    raf = new RandomAccessFile(file, "r");
                    FileChannel channel = raf.getChannel();
                    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
                    pdffile = new PDFFile(buf);
                    pdffile.stop(1);
   
                    try {
                        book.setValue(Book._T_NROFPAGES, Long.valueOf(pdffile.getNumPages()));
View Full Code Here

    //File fileXML = new File("/users/epsobolik/documents/SchbAvMaSample.xml");
    try {
        FileInputStream fisXML = new FileInputStream(fileXML);
       
        //  test the XML file by displaying the first 1024 characters
      FileChannel fc = fisXML.getChannel();
      ByteBuffer buf = ByteBuffer.allocate((int)fc.size());
      fc.read(buf);
      buf.flip();
  //      while(buf.hasRemaining())
  //        System.out.print((char)buf.get());
  //      fisXML.close();
  //      System.out.print('\n');
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.