Android Open Source - you2peer File Answer






From Project

Back to project page you2peer.

License

The source code is released under:

Apache License

If you think the Android project you2peer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.u2p.messages;
//from  w  ww  .  j  a  v  a2s.c o  m
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import android.util.Log;

public class FileAnswer implements Serializable{

  private static final long serialVersionUID = 1L;
  private static final String TAG="ListAnswer";
  private String filename;
  private String group;
  private byte[] bytes;
  
  public FileAnswer(String filename,String group){
    this.filename=filename;
    this.group=group;
  }
  
  public String getGroup(){
    return this.group;
  }
  
  public String getFilename(){
    return filename;
  }
  
  public void read(String uri){
    File file = new File(uri);
    bytes = new byte[(int)file.length()];
    try {
      InputStream input = null;
      try {
        int totalBytesRead = 0;
        input = new BufferedInputStream(new FileInputStream(file));
        while(totalBytesRead < bytes.length){
          int bytesRemaining = bytes.length - totalBytesRead;
          //input.read() returns -1, 0, or more :
          int bytesRead = input.read(bytes, totalBytesRead, bytesRemaining); 
          if (bytesRead > 0){
            totalBytesRead = totalBytesRead + bytesRead;
          }
        }
        /*
             the above style is a bit tricky: it places bytes into the 'result' array; 
             'result' is an output parameter;
             the while loop usually has a single iteration only.
         */
      }
      finally {
        input.close();
      }
    }
    catch (FileNotFoundException ex) {
      Log.e(TAG,"File not found");
    }
    catch (IOException ex) {
      Log.e(TAG,"IOException");
    }
  }
  
  public void write(String uri){
    try {
      OutputStream output = null;
      try {
        output = new BufferedOutputStream(new FileOutputStream(uri+"/"+filename));
        output.write(bytes);
      }
      finally {
        output.close();
      }
    }
    catch(FileNotFoundException ex){
      Log.e(TAG,"File not found");
    }
    catch(IOException ex){
      Log.e(TAG,"IOException");
    }
  }

  
}




Java Source Code List

com.u2p.core.comm.Client.java
com.u2p.core.comm.Server.java
com.u2p.core.db.DbDataSource.java
com.u2p.core.db.DbFile.java
com.u2p.core.db.DbGroups.java
com.u2p.core.db.DbU2P.java
com.u2p.core.db.DbUser.java
com.u2p.core.nsd.NsdHelper.java
com.u2p.events.ActivityEventsGenerator.java
com.u2p.events.ActivityEventsListener.java
com.u2p.events.ActivityEvents.java
com.u2p.events.FileEvent.java
com.u2p.events.ListEvent.java
com.u2p.events.NewClientEvent.java
com.u2p.events.NewGroupList.java
com.u2p.events.ServerEventsGenerator.java
com.u2p.events.ServerEventsListener.java
com.u2p.events.VoteEvent.java
com.u2p.messages.ACK.java
com.u2p.messages.Authentication.java
com.u2p.messages.FileAnswer.java
com.u2p.messages.FileRequest.java
com.u2p.messages.ListAnswer.java
com.u2p.messages.ListRequest.java
com.u2p.messages.NewFile.java
com.u2p.messages.StillAlive.java
com.u2p.messages.VoteFile.java
com.u2p.ui.FileDetailsActivity.java
com.u2p.ui.FileSelectionActivity.java
com.u2p.ui.MainActivity.java
com.u2p.ui.adapters.ItemFileAdapter.java
com.u2p.ui.component.GroupListFile.java
com.u2p.ui.component.ItemFile.java
com.u2p.ui.component.LoginDialogFragment.java