Examples of FileChannel


Examples of java.nio.channels.FileChannel

  }
 
  private void loadUserHash() throws Throwable {
    File file = new File(USER_HASH_FILE);
    if (!file.exists()) return ;
    FileChannel input_channel = new FileInputStream(file).getChannel();
    ByteBuffer hash = Misc.getByteBuffer(16);
    input_channel.read(hash);
    user_hash = new UserHash(hash.array());
    input_channel.close();
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

    user_hash = new UserHash(hash.array());
    input_channel.close();
  }
 
  private void storeClientID() throws Throwable {
    FileChannel output_channel = new FileOutputStream(KAD_ID_FILE).getChannel();
    ByteBuffer data = Misc.getByteBuffer(16);
    data.put(client_id.toByteArray());
    data.position(0);
    output_channel.write(data);
    output_channel.close();
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

  }
 
  private void loadClientID() throws Throwable {
    File file = new File(KAD_ID_FILE);
    if (!file.exists()) return ;
    FileChannel input_channel = new FileInputStream(file).getChannel();
    ByteBuffer data = Misc.getByteBuffer(16);
    input_channel.read(data);
    client_id = new ClientID(data.array());
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

   * @param destination
   * @throws Exception
   * @deprecated
   */
  public static void copyFile(String source, String destination) throws Exception {
        FileChannel srcChannel = new FileInputStream(source).getChannel();
   
        FileChannel dstChannel = new FileOutputStream(destination).getChannel();
   
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
   
        srcChannel.close();
        dstChannel.close();
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

    MyClosure readFileChannel = new MyClosure() {
      public void run(String filename) throws IOException {
        if (show) out.format("read %s %n", filename);
        FileInputStream fis = new FileInputStream(filename);
        FileChannel fc = fis.getChannel();
        nbytes += IO.copy2null(fc, buffersize);
        nfiles++;
        fis.close();
      }
    };
View Full Code Here

Examples of java.nio.channels.FileChannel

          final long FRAME = 32000;
          final long STEPBACK = FROM__PATTERN.length() - 1;
          long size = (length<FRAME ? length : FRAME);

          long offset = 0;
          FileChannel chnnl = getChannel();

          // read mbox file to determine the message positions..
          ByteBuffer buffer = chnnl.map(FileChannel.MapMode.READ_ONLY, 0l, size);
          CharBuffer cb = decoder.decode(buffer);

          // check that first message is correct..
          if (Pattern.compile(INITIAL_FROM__PATTERN, Pattern.DOTALL).matcher(cb).matches()) {
            // debugging..
            log.debug("Matched first message...");

            posList.add(new Long(0));
          }

          Pattern fromPattern = Pattern.compile(FROM__PATTERN);
          Matcher matcher;

          do {
            log.debug("scanning from " + String.valueOf(offset) + " to " + String.valueOf(offset+size));
            matcher = fromPattern.matcher(cb);
            while (matcher.find()) {
                // log.debug("Found match at [" + String.valueOf(offset+matcher.start()) + "]");

                // add one (1) to position to account for newline..
                posList.add(new Long(offset+matcher.start() + 1));
            } // wend

            if (size<FRAME) break;

            offset  += FRAME-STEPBACK;
            size = (offset+FRAME<length) ? FRAME : length-(offset+1);

            buffer = chnnl.map(FileChannel.MapMode.READ_ONLY, offset, size);
            cb = decoder.decode(buffer);
          } while (true);

          log.debug("found " + String.valueOf(posList.size()) + " matches");
View Full Code Here

Examples of java.nio.channels.FileChannel

    setContentType(getContentTypeByFileExtension(file));
    setContentLength((int) file.length());
    removeHeader("Transfer-Encoding");
   
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel fc = raf.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(getContentLength());
    fc.read(buffer);
    fc.close();
    raf.close();
    buffer.flip();
   
    getNonBlockingBody().append(true, buffer);
    getNonBlockingBody().setComplete(true);
View Full Code Here

Examples of java.nio.channels.FileChannel

    return new String(b).equals("TAG");
  }

  public void write(Tag tag, RandomAccessFile raf) throws IOException {
    FileChannel fc = raf.getChannel();

    ByteBuffer tagBuffer = tc.convert(tag);

    if (!tagExists(raf)) {
      //System.err.println("Creating a new ID3v1 Tag");
      fc.position(fc.size());
      fc.write(tagBuffer);
      //ID3v1 Tag Written
    } else {
      //System.err.println("Old ID3v1 Tag found, replacing the old tag");
      fc.position(fc.size() - 128);
      fc.write(tagBuffer);
    }
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

    long oldFileSize = fileChannel.size(); // includes overflow buckets

    // Move any overflow buckets out of the way to a temporary file
    File tmpFile = new File(file.getParentFile(), "rehash_" + file.getName());
    RandomAccessFile tmpRaf = createEmptyFile(tmpFile);
    FileChannel tmpChannel = tmpRaf.getChannel();

    // Transfer the overflow buckets to the temp file
    fileChannel.transferTo(oldTableSize, oldFileSize, tmpChannel);

    // Increase hash table by factor 2
    writeEmptyBuckets(oldTableSize, bucketCount);
    bucketCount *= 2;

    // Discard any remaining overflow buffers
    fileChannel.truncate(newTableSize);

    ByteBuffer bucket = ByteBuffer.allocate(recordSize);
    ByteBuffer newBucket = ByteBuffer.allocate(recordSize);

    // Rehash items in 'normal' buckets, half of these will move to a new
    // location, but none of them will trigger the creation of new overflow
    // buckets. Any (now deprecated) references to overflow buckets are
    // removed too.

    // All items that are moved to a new location end up in one and the same
    // new and empty bucket. All items are divided between the old and the new
    // bucket and the changes to the buckets are written to disk only once.
    for (long bucketOffset = HEADER_LENGTH; bucketOffset < oldTableSize; bucketOffset += recordSize) {
      fileChannel.read(bucket, bucketOffset);

      boolean bucketChanged = false;
      long newBucketOffset = 0L;

      for (int slotNo = 0; slotNo < bucketSize; slotNo++) {
        int id = bucket.getInt(ITEM_SIZE * slotNo + 4);

        if (id != 0) {
          // Slot is not empty
          int hash = bucket.getInt(ITEM_SIZE * slotNo);
          long newOffset = getBucketOffset(hash);

          if (newOffset != bucketOffset) {
            // Move this item to new bucket...
            newBucket.putInt(hash);
            newBucket.putInt(id);

            // ...and remove it from the current bucket
            bucket.putInt(ITEM_SIZE * slotNo, 0);
            bucket.putInt(ITEM_SIZE * slotNo + 4, 0);

            bucketChanged = true;
            newBucketOffset = newOffset;
          }
        }
      }

      if (bucketChanged) {
        // Some of the items were moved to the new bucket, write it to the
        // file
        newBucket.flip();
        fileChannel.write(newBucket, newBucketOffset);
        newBucket.clear();
      }

      // Reset overflow ID in the old bucket to 0 if necessary
      if (bucket.getInt(ITEM_SIZE * bucketSize) != 0) {
        bucket.putInt(ITEM_SIZE * bucketSize, 0);
        bucketChanged = true;
      }

      if (bucketChanged) {
        // Some of the items were moved to the new bucket or the overflow
        // ID has been reset; write the bucket back to the file
        bucket.rewind();
        fileChannel.write(bucket, bucketOffset);
      }

      bucket.clear();
    }

    // Rehash items in overflow buckets. This might trigger the creation of
    // new overflow buckets so we can't optimize this in the same way as we
    // rehash the normal buckets.
    long tmpFileSize = tmpChannel.size();
    for (long bucketOffset = 0L; bucketOffset < tmpFileSize; bucketOffset += recordSize) {
      tmpChannel.read(bucket, bucketOffset);

      for (int slotNo = 0; slotNo < bucketSize; slotNo++) {
        int id = bucket.getInt(ITEM_SIZE * slotNo + 4);

        if (id != 0) {
View Full Code Here

Examples of java.nio.channels.FileChannel

    }
   

  static ByteBuffer[] readFile(File file) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate((int) fc.size());
        fc.read(buffer);
        fc.close();
        raf.close();
        buffer.flip();
       
        return new ByteBuffer[] { buffer };
    }
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.