Examples of RandomAccessFile


Examples of java.io.RandomAccessFile

    private static void testFile(String mode, boolean flush) throws Exception {
        System.out.println("Testing RandomAccessFile(.., \"" + mode + "\")...");
        if (flush) {
            System.out.println("  with FileDescriptor.sync()");
        }
        RandomAccessFile file = new RandomAccessFile("test.txt", mode);
        file.setLength(0);
        FileDescriptor fd = file.getFD();
        long start = System.currentTimeMillis();
        byte[] data = { 0 };
        file.write(data);
        int i = 0;
        if (flush) {
            for (;; i++) {
                file.seek(0);
                file.write(data);
                fd.sync();
                if ((i & 15) == 0) {
                    long time = System.currentTimeMillis() - start;
                    if (time > 5000) {
                        break;
                    }
                }
            }
        } else {
            for (;; i++) {
                file.seek(0);
                file.write(data);
                if ((i & 1023) == 0) {
                    long time = System.currentTimeMillis() - start;
                    if (time > 5000) {
                        break;
                    }
                }
            }
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("Time: " + time);
        System.out.println("Operations: " + i);
        System.out.println("Operations/second: " + (i * 1000 / time));
        System.out.println();
        file.close();
        new File("test.txt").delete();
    }
View Full Code Here

Examples of java.io.RandomAccessFile

        f.setReadOnly();
        assertTrue(!f.canWrite());
        f.delete();

        f = File.createTempFile("test", "temp");
        RandomAccessFile r = new RandomAccessFile(f, "rw");
        r.write(1);
        f.setReadOnly();
        r.close();
        assertTrue(!f.canWrite());
        f.delete();

        deleteDb("readonly");
        Connection conn = getConnection("readonly");
View Full Code Here

Examples of java.io.RandomAccessFile

        BTreePersistentIndexedCache<String, Integer> cache = new BTreePersistentIndexedCache<String, Integer>(backingCache, serializer);

        assertNull(cache.get("key_1"));
        cache.put("key_1", 99);

        RandomAccessFile file = new RandomAccessFile(testFile, "rw");
        file.setLength(file.length() - 10);

        cache.reset();

        assertNull(cache.get("key_1"));
        cache.verify();
View Full Code Here

Examples of java.io.RandomAccessFile

            this.mode = MapMode.READ_ONLY;
        } else {
            this.mode = MapMode.READ_WRITE;
        }
        this.name = fileName;
        file = new RandomAccessFile(fileName, mode);
        reMap();
    }
View Full Code Here

Examples of java.io.RandomAccessFile

        transformers = new int[1];
        transformers[0] = XmlTransformers._HTML;
       
        File file = getFile();
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            long length = raf.length();
           
            while (raf.getFilePointer() < length) {
                String line = raf.readLine();
                if (line.toLowerCase().indexOf("xmlns:fo=\"http://www.w3.org/1999/xsl/format\"") > -1) {
                    transformers = new int[2];
                    transformers[0] = XmlTransformers._PDF;
                    transformers[1] = XmlTransformers._RTF;
                }
            }
           
            raf.close();
        } catch (IOException e) {
            logger.error("Could not determine if PDF / RTF is supported for file " + file , e);
        }
    }
View Full Code Here

Examples of java.io.RandomAccessFile

        throw( new IOException( "destroyed" ));
      }
     
      if ( raf == null ){
           
        raf = new RandomAccessFile( source_file, "r" );
      }
     
      raf_count++;
     
      return( raf );
View Full Code Here

Examples of java.io.RandomAccessFile

    new AEThread2( "TranscodePipe:c", true )
    {
      public void
      run()
      {
        RandomAccessFile  raf = null;
       
        try{
          raf = reserveRAF();

          long  pos  = position;
         
          long  rem = length;
         
          while( !destroyed && rem > 0){
                                 
            int  limit;

            if ( paused ){
             
              Thread.sleep(250);
             
              limit = 1;
             
            }else{
                         
              if ( max_bytes_per_sec > 0 ){
               
                limit = bytes_available;
               
                if ( limit <= 0 ){
                 
                  Thread.sleep( 25 );
                 
                  continue;
                }
               
                limit = Math.min( BUFFER_SIZE, limit );
               
              }else{
               
                limit = BUFFER_SIZE;
              }
             
              limit = (int)Math.min( rem, limit );
            }
           
            int  read_length  = 0;
           
            int    buffer_start   = 0;
            byte[]   buffer      = null;
           
            synchronized( TranscodePipe.this ){
           
              int  c_num = 0;
             
              Iterator<bufferCache> it = buffer_cache.iterator();
             
              while( it.hasNext()){
               
                bufferCache b = it.next();
               
                long  rel_offset = pos - b.offset;
               
                if ( rel_offset >= 0 ){
                 
                  byte[]   data = b.data;
                 
                  long avail = data.length - rel_offset;
                 
                  if ( avail > 0 ){
                   
                    read_length = (int)Math.min( avail, limit );
                   
                    buffer       = data;
                    buffer_start   = (int)rel_offset;
                   
                    //System.out.println( "using cache entry: o_offset=" + pos + ",r_offset=" + rel_offset+",len=" + read_length );
                   
                    if ( c_num > 0 ){
                   
                      it.remove();
                   
                      buffer_cache.addFirst( b );
                    }
                   
                    break;
                  }
                }
               
                c_num++;
              }
             
              if ( buffer == null ){
             
                buffer = new byte[ limit ];

                raf.seek( pos );
             
                read_length =  raf.read( buffer );
               
                if ( read_length != limit ){
                 
                  Debug.out( "eh?");
                 
                  throw( new IOException( "Inconsistent" ));
                }
               
                bufferCache b = new bufferCache( pos, buffer );
               
                // System.out.println( "adding to cache: o_offset=" + pos + ", size=" + limit );
               
                buffer_cache.addFirst( b );
               
                buffer_cache_size += limit;
               
                while( buffer_cache_size > BUFFER_CACHE_SIZE ){
                 
                  b = buffer_cache.removeLast();
                 
                  buffer_cache_size -= b.data.length;
                }
              }
            }
             
            if ( read_length <= 0 ){
               
              break;
            }
             
            rem -= read_length;
            pos += read_length;
           
            if ( max_bytes_per_sec > 0 ){
           
              bytes_available -= read_length;
            }
           
            os.write( buffer, buffer_start, read_length );   
           
            write_speed.addValue( read_length );
          }
         
          os.flush();
         
        }catch( Throwable e ){
       
          if ( raf != null ){
           
            try{
              synchronized( TranscodePipe.this ){

                raf.seek( 0 );
             
                raf.read( new byte[1] );
              }
            }catch( Throwable f ){
             
              reportError( e );
            }
View Full Code Here

Examples of java.io.RandomAccessFile

    this.r = read;
    this.w = write;
    String mode = "";
    if(this.r) { mode += "r"; }
    if(this.w) { mode += "w"; }
    this.delegate = new RandomAccessFile(file, mode);
  }
View Full Code Here

Examples of java.io.RandomAccessFile

     * @throws DiskCacheException if any exceptioins occur.
     */
    public CacheFileAdapter(final File file) {
        this.filepath = file.getAbsolutePath();
        try {
            raf = new RandomAccessFile(filepath, "rw");
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(
                "The disk cache could not be initialized.");
        }
    }
View Full Code Here

Examples of java.io.RandomAccessFile

      } catch (InterruptedException e1) {
        //interrupted.
      }
        }
        try {
            raf = new RandomAccessFile(filepath, "rw");
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e.getMessage());
        }
    }
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.