Package java.io

Examples of java.io.RandomAccessFile


        //size (in bytes)
        //offset (in bytes)
        //challenge_id
        Map retVal = new HashMap();
        RandomAccessFile raf=null;
        try{

            Long size = (Long) result.get("size");
            Long offset = (Long) result.get("offset");
            if( size==null || offset==null  )
                throw new IllegalStateException("scheduleTestWithSpeedTestService had a null parameter.");

            //read the bytes
            raf = new RandomAccessFile( jar_file, "r" );
            byte[] jarBytes = new byte[size.intValue()];

            raf.seek(offset.intValue());
            raf.read( jarBytes );


            //Build the URL.
            Map request = new HashMap();
            request.put("request_type", new Long(CHALLENGE_REPLY) );
            request.put("challenge_id", challenge_id );
            request.put("data",jarBytes);
            retVal = sendRequest( request );

        }finally{
            //close
            try{
                if(raf!=null)
                    raf.close();
            }catch(Throwable t){
                Debug.printStackTrace(t);
            }
        }
View Full Code Here


              return;
            }
           
            scratch_file = AETemporaryFileHandler.createTempFile();

            raf = new RandomAccessFile( scratch_file, "rw" );
          }
                   
          // System.out.println( "Connecting to " + url + ": " + Thread.currentThread().getId());
 
          HttpURLConnection  connection;
View Full Code Here

        // if the subsequent open fails
    }
   
    file_access.aboutToOpen();
   
    raf = new RandomAccessFile( linked_file, access_mode==FM_READ?READ_ACCESS_MODE:WRITE_ACCESS_MODE);
   
    Debug.outNoStack( "Recovered connection to " + getName() + " after access failure" );
  }
View Full Code Here

    reserveAccess( reason );
   
    try
      file_access.aboutToOpen();
     
      raf = new RandomAccessFile( linked_file, access_mode==FM_READ?READ_ACCESS_MODE:WRITE_ACCESS_MODE);
     
    }catch( Throwable e ){
     
      Debug.printStackTrace( e );
     
View Full Code Here

      throw( new FMFileManagerException( "Conversion to/from piece-reorder not supported" ));
    }
   
    File  file = owner.getLinkedFile();
     
    RandomAccessFile raf = null;
   
    boolean  ok  = false;
   
    try{
      FMFileAccess  target_access;
     
      if ( target_type == FMFile.FT_LINEAR ){
       
        target_access = new FMFileAccessLinear( owner );
                 
      }else{
       
        target_access = new FMFileAccessCompact(
                    owner.getOwner().getTorrentFile(),control_dir,
                    controlFileName, 
                    new FMFileAccessLinear( owner ));
      }

      if ( file.exists()){

        raf = new RandomAccessFile( file, FMFileImpl.WRITE_ACCESS_MODE);
       
          // due to the simplistic implementation of compact we only actually need to deal with
          // the last piece of the file (first piece is in the right place already)
       
        FMFileAccessCompact  compact_access;
       
        if ( target_type == FMFile.FT_LINEAR ){
                   
          compact_access = (FMFileAccessCompact)file_access;
           
        }else{
         
           compact_access = (FMFileAccessCompact)target_access;
        }
       
        long  length = file_access.getLength( raf );
       
        long  last_piece_start   = compact_access.getLastPieceStart();
        long  last_piece_length   = compact_access.getLastPieceLength();
       
          // see if we have any potential data for the last piece
       
        if ( last_piece_length > 0 && length > last_piece_start ){
         
          long  data_length = length - last_piece_start;
         
          if ( data_length > last_piece_length ){
           
            Debug.out("data length inconsistent: len=" + data_length + ",limit=" + last_piece_length );
           
            data_length = last_piece_length;
          }
         
          DirectByteBuffer  buffer =
            DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_FILE, (int)data_length );
         
          try{
         
            file_access.read( raf, new DirectByteBuffer[]{ buffer }, last_piece_start );
           
              // see if we need to truncate
           
            if ( target_type == FMFile.FT_COMPACT ){
             
              long  first_piece_length = compact_access.getFirstPieceLength();
             
              long  physical_length = raf.length();
             
              if ( physical_length > first_piece_length ){
           
                raf.setLength( first_piece_length );
              }
            }
           
            buffer.flip( DirectByteBuffer.AL_FILE );
           
            target_access.write( raf, new DirectByteBuffer[]{ buffer }, last_piece_start );
           
          }finally{
           
            buffer.returnToPool();
          }
        }else{
         
            // no last piece, truncate after the first piece
         
          if ( target_type == FMFile.FT_COMPACT ){
           
            long  first_piece_length = compact_access.getFirstPieceLength();
           
            long  physical_length = raf.length();
           
            if ( physical_length > first_piece_length ){
         
              raf.setLength( first_piece_length );
            }
          }
        }
         
        target_access.setLength( raf, length );
       
        target_access.flush();
      }
     
      type    = target_type;
      file_access  = target_access;
 
      ok  = true;
     
    }catch( Throwable e ){
     
      Debug.printStackTrace( e );
     
      throw( new FMFileManagerException( "convert fails", e ));
     
    }finally{
     
      try{
        if ( raf != null ){
         
          try{
            raf.close();
           
          }catch( Throwable e ){
         
              // override original exception if there isn't one
           
View Full Code Here

                        book.setValue(Book._N_ISBN13, ISBN.isISBN10(s) ? ISBN.getISBN13(s) :
                                                      ISBN.isISBN13(s) ? s : null);
                }
               
               
                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()));
                        Iterator<String> it = pdffile.getMetadataKeys();
                        while (it.hasNext()) {
                            String key = it.next();
                            String value = pdffile.getStringMetadata(key);
                           
                            if (!Utilities.isEmpty(value)) {
                                if (key.equalsIgnoreCase("Author"))
                                    DataManager.createReference(book, Book._G_AUTHOR, value);
                                if (key.equalsIgnoreCase("Title") && !value.trim().equalsIgnoreCase("untitled"))
                                    book.setValue(Book._A_TITLE, value);
                            }
                        }
                    } catch (IOException ioe) {
                        getClient().addMessage(DcResources.getText("msgCouldNotReadInfoFrom", filename));
                    }
   
                    // draw the first page to an image
                    PDFPage page = pdffile.getPage(0);
                    if (page != null) {
                        Rectangle rect = new Rectangle(0,0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight());
                        Image front = page.getImage(rect.width, rect.height, rect, null, true, true);
                        book.setValue(Book._K_PICTUREFRONT, new DcImageIcon(Utilities.getBytes(new DcImageIcon(front))));
                    }
                } finally {
                    if (raf != null)
                        raf.close();
                   
                }
            }
           
            Hash.getInstance().calculateHash(book);
View Full Code Here

     */
    public MovieFile(String file) {
        String filename = file;
       
        try {
            raf = new RandomAccessFile(filename, "r");
            // check whether its really an avi file
            read_str(2);
            File oFile = new File(filename);
            filename = oFile.getName();
            movieFileName = filename;
View Full Code Here

    private void fillQueryComboBox() {
        File queryFile = new File(DataCrow.dataDir, "data_crow_queries.txt");

        try {
            if (queryFile.exists()) {
                RandomAccessFile fileAccess  = new RandomAccessFile(queryFile, "rw");
                long filePointer = 0;
                long fileLength  = queryFile.length();
                comboSQLCommands.addItem("");
                while (filePointer < fileLength) {
                    String query = fileAccess.readLine();
                    if (query != null) {
                        addQueryToComboBox(query.trim());
                        filePointer = fileAccess.getFilePointer();
                    }
                }
                fileAccess.close();
            } else if (!queryFile.exists()) {
                logger.info(DcResources.getText("msgFileNotFound", queryFile.toString()));
                RandomAccessFile fileAccess  = new RandomAccessFile(queryFile, "rw");
                fileAccess.write(DcRepository.Database._PREDEFINEDQRY.getBytes());
                fileAccess.close();
                fillQueryComboBox();
            }
        } catch (Exception e) {
            comboSQLCommands.addItem("");
            logger.error("Could not read " + queryFile, e);
View Full Code Here

            return false;
        }
        // File.canWrite() does not respect windows user permissions,
        // so we must try to open it using the mode "rw".
        // See also http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4420020
        RandomAccessFile r = null;
        try {
            r = new RandomAccessFile(file, "rw");
            return true;
        } catch (FileNotFoundException e) {
            return false;
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
View Full Code Here

    private FileLock lock;
    private long length;

    FileObjectDiskChannel(String fileName, String mode) throws IOException {
        this.name = fileName;
        file = new RandomAccessFile(fileName, mode);
        channel = file.getChannel();
        length = file.length();
    }
View Full Code Here

TOP

Related Classes of java.io.RandomAccessFile

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.