Examples of FileChannel


Examples of java.nio.channels.FileChannel

    return true;
  }
 
  public static void writeBytesToFile(byte[] bytes, String destination) {
    try {
      FileChannel fc = new FileOutputStream(destination).getChannel();
      fc.write(ByteBuffer.wrap(bytes));
      fc.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

   * @exception XException if any error occurs
   */
  protected void copyFile(String sourceFile, String targetFile)
      throws XException
  {
    FileChannel srcChannel = null;
    FileOutputStream fos = null;
    FileChannel dstChannel = null;

    try
    {
      // Get a file channel for the SourceFile
      File lockSourceFile = new File(sourceFile);
      srcChannel = new RandomAccessFile(lockSourceFile, "r")
          .getChannel();

      // Create channel on the destination
      fos = new FileOutputStream(targetFile);
      dstChannel = fos.getChannel();

      // Copy file contents from source to destination
      dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

    }
    catch (IOException e)
    {
      throw new XException(Constants.LOCATION_EXTERN,
          Constants.LAYER_TECHNICAL,
          Constants.PACKAGE_TECHNICAL_FILE, "0", e);
    }
    finally
    {
      try
      {
        if (srcChannel != null)
        {
          srcChannel.close();
        }
        if (fos != null)
        {
          fos.close();
        }
        if (dstChannel != null)
        {
          dstChannel.close();
        }
      }
      catch (IOException e)
      {
        throw new XException(Constants.LOCATION_EXTERN,
View Full Code Here

Examples of java.nio.channels.FileChannel

        FutureResponseHandler respHdl = new FutureResponseHandler();
        BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
       
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();
        dataSink.transferFrom(fc);
        fc.close();
        raf.close();
        dataSink.close();
       
        IHttpResponse response = respHdl.getResponse();
       
View Full Code Here

Examples of java.nio.channels.FileChannel

        FutureResponseHandler respHdl = new FutureResponseHandler();
        BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
       
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();
        dataSink.transferFrom(fc);
        fc.close();
        raf.close();
        dataSink.close();
       
        IHttpResponse response = respHdl.getResponse();
       
View Full Code Here

Examples of java.nio.channels.FileChannel

*/
public class SrcIndexDat {

  public static  Map<Int128, Index> loadFile(String fileName) throws Throwable {
    Map<Int128, Index> result = new Hashtable<Int128, Index>();
    FileChannel channel = new RandomAccessFile(fileName,"rw").getChannel();
   
    channel.position(4+4);
   
    ByteBuffer data;
   
    data = getByteBuffer(4);
   
    channel.read(data);
    int count = data.getInt(0);
   
    for(int i=0;i<count;i++) {
      data = getByteBuffer(16);
      channel.read(data);
      Int128 key_id = new Int128(data.array());
      data = getByteBuffer(4);
      channel.read(data);
      Index index = new Index(key_id);
      int source_count = data.getInt(0);
   
      for(int j = 0;j<source_count;j++) {
        data = getByteBuffer(16);
        channel.read(data);
        ClientID client_id = new ClientID(data.array());

        data = getByteBuffer(8);
        channel.read(data);
        long creation_time = data.getLong(0);
        data = getByteBuffer(1);
        channel.read(data);
        int tagCount = data.get(0);
        TagList tagList = new TagList();
       
        for(int k = 0;k<tagCount;k++) {
          Tag tag = TagScanner.scanTag(channel);
View Full Code Here

Examples of java.nio.channels.FileChannel

   
    return result;
  }
 
  public static void writeFile(String fileName, Map<Int128, Index> sourceData) throws Throwable {
    FileChannel channel = new FileOutputStream(fileName).getChannel();
    ByteBuffer data = getByteBuffer(4);
   
    data.put(SRC_INDEX_VERSION);
    data.position(0);
    channel.write(data);
   
    data.position(0);
    data.putInt(Convert.longToInt(System.currentTimeMillis()));
    data.position(0);
    channel.write(data);
   
    data.position(0);
    data.putInt(sourceData.size());
    data.position(0);
    channel.write(data);
   
    for(Int128 key : sourceData.keySet()) {
      data = getByteBuffer(16);
      data.put(key.toByteArray());
      data.position(0);
      channel.write(data);
     
      Index index = sourceData.get(key);
      data = getByteBuffer(4);
      data.putInt(index.getSourceList().size());
      data.position(0);
      channel.write(data);
     
      for(Source source : index.getSourceList()) {
        data = getByteBuffer(16);
        data.put(source.getClientID().toByteArray());
        data.position(0);
        channel.write(data);
       
        data = getByteBuffer(8);
        data.putLong(0, source.getCreationTime());
        data.position(0);
        channel.write(data);
       
        data = getByteBuffer(1);
        data.put(Convert.intToByte(source.getTagList().size()));
        data.position(0);
        channel.write(data);
        for(Tag tag : source.getTagList()) {
          ByteBuffer buffer = tag.getAsByteBuffer();
          buffer.position(0);
          channel.write(buffer);
        }
      }
    }
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

   */
  public static List<KadContact> loadFile(String fileName) {
    List<KadContact> result = new LinkedList<KadContact>();
   
    try {
      FileChannel channel = new RandomAccessFile(fileName,"rw").getChannel();
     
      ByteBuffer data = getByteBuffer(4);
      channel.position(4); // skip 'old' contacts count field
     
      channel.read(data); // nodes.dat version
     
      data.position(0);
      channel.read(data);
     
      int totalContacts = data.getInt(0);
     
      for(int i = 1 ; i <= totalContacts ; i++) {
       
        data = getByteBuffer(16);
        channel.read(data);
        ClientID contact_id = new ClientID(data.array());
        data = getByteBuffer(4);
        channel.read(data);
        byte[] ip = data.array().clone();
        //ip = Convert.reverseArray(ip);
        IPAddress address = new IPAddress(ip);
       
        data = getByteBuffer(2);
        channel.read(data);
        short udp_port = data.getShort(0);
       
        data = getByteBuffer(2);
        channel.read(data);
        short tcp_port = data.getShort(0);
       
        data = getByteBuffer(1);
        channel.read(data);
        byte contact_version = data.get(0);
       
        data = getByteBuffer(4);
        channel.read(data);
       
        ByteBuffer data2 = getByteBuffer(4);
        channel.read(data2);
       
        JKadUDPKey udp_key = new JKadUDPKey(data.array(), data2.array());
       
        data = getByteBuffer(1);
        channel.read(data);

        if (Utils.isGoodAddress(address)) {
          KadContact contact = new KadContact(contact_id, new ContactAddress(address, Convert.shortToInt(udp_port)), Convert.shortToInt(tcp_port), contact_version, udp_key, data.get(0)==1 ? true : false);
         
          result.add(contact);
        }
      }
     
      channel.close();
    }catch(Throwable t) {
      t.printStackTrace();
    }
   
    return result;
View Full Code Here

Examples of java.nio.channels.FileChannel

        current_hashing_file = null;
        if (stop)
          return;
        try {
          current_hashing_file = shared_completed_file;
          FileChannel file_channel = new FileInputStream(
              shared_completed_file.getFile()).getChannel();
          file_hashing = new FileHashing(file_channel);
          file_hashing.start();
          file_hashing.join();
          file_channel.close();
          files_needed_to_hash.remove(shared_completed_file);
          if (!file_hashing.isDone())
            continue;
          if (sharedFiles.containsKey(file_hashing.getFileHashSet()
              .getFileHash()))
View Full Code Here

Examples of java.nio.channels.FileChannel

   * @param fileName
   * @param contactList
   */
  public static void writeFile(String fileName, List<KadContact> contactList) {
    try {
      FileChannel channel = new FileOutputStream(fileName).getChannel();
      ByteBuffer data = getByteBuffer(4);
     
      channel.write(data);
     
      data.position(0);
      data.put(NODES_DAT_VERSION);
      data.position(0);
      channel.write(data);
     
      data.position(0);
      data.putInt(contactList.size());
      data.position(0);
      channel.write(data);
     
      for(KadContact contact : contactList) {
        data = getByteBuffer(16);
        data.put(contact.getContactID().toByteArray());
        data.position(0);
        channel.write(data);
       
        data = getByteBuffer(4);
        data.put(contact.getIPAddress().getAddress());
        data.position(0);
        channel.write(data);
       
        data  = getByteBuffer(2);
        data.putShort(intToShort(contact.getUDPPort()));
        data.position(0);
        channel.write(data);
       
        data  = getByteBuffer(2);
        data.putShort(intToShort(contact.getTCPPort()));
        data.position(0);
        channel.write(data);
       
        data  = getByteBuffer(1);
        data.put(contact.getVersion());
        data.position(0);
        channel.write(data);
       
        // write key
        JKadUDPKey key = contact.getKadUDPKey();
        if (key == null) {
          data  = getByteBuffer(4 + 4);
          data.position(0);
          channel.write(data);
        } else {
          data  = getByteBuffer(4 + 4);
          data.put(key.getKey());
          data.put(key.getAddress().getAddress());
          data.position(0);
          channel.write(data);
        }
       
        data = getByteBuffer(1);
        data.put((byte)(contact.isIPVerified() ? 1 : 0));
        data.position(0);
        channel.write(data);
      }
     
      channel.close();
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

    }
    notifyPropertyChanged(JKAD_ID_KEY, newID);
  }
 
  private void storeUserHash() throws Throwable {
    FileChannel output_channel = new FileOutputStream(USER_HASH_FILE).getChannel();
    ByteBuffer hash = Misc.getByteBuffer(16);
    hash.put(user_hash.getUserHash());
    hash.position(0);
    output_channel.write(hash);
    output_channel.close();
  }
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.