Package transfer.upload

Source Code of transfer.upload.FileUploadConnection

package transfer.upload;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;

import main.PeerProtocol;

import common.connection.ConnectionWriterInterface;
import common.files.SerializableFile;
import common.handlers.AbstractHandlerSet;
import common.handlers.EmptyHandlerSet;

import connection.peer.GenericPeerConnection;

import static main.ClientMain.*;


public class FileUploadConnection extends GenericPeerConnection
{
  private static final EmptyHandlerSet HANDLER_SET = new EmptyHandlerSet();

  private FileUpload upload;
  private InputStream fis;

  public FileUploadConnection(GenericPeerConnection source, SerializableFile file)
      throws IOException
  {
    super(source);
    upload = new FileUpload(file, this);
    fis = new BufferedInputStream(getSharedFilesTable().openFile(upload.getFile()));
  }

  public void initializeConnection() throws IOException
  {
    upload.addToManager();
  }

  public void finalizeConnection()
  {
    upload.removeFromManager();
  }

  protected AbstractHandlerSet getHandlerSet()
  {
    return HANDLER_SET;
  }

  public void upload(long offset) throws IOException
  {
    upload.setCompletedSize(offset);

    int protocolId = PeerProtocol.PROTOCOL_TRANSFER;
    int opcode = PeerProtocol.Transfer.OP_FILE;
    send(protocolId, opcode, new ConnectionWriterInterface()
    {
      public void run(ObjectOutputStream os) throws IOException
      {
        upload.transfer(fis, os);
      }

    });
  }

}
TOP

Related Classes of transfer.upload.FileUploadConnection

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.