FileDownloadProvider.java :  » Web-Framework » roma-webwizard » org » romaframework » aspect » view » echo2 » Java Open Source

Java Open Source » Web Framework » roma webwizard 
roma webwizard » org » romaframework » aspect » view » echo2 » FileDownloadProvider.java
package org.romaframework.aspect.view.echo2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import nextapp.echo2.app.filetransfer.AbstractDownloadProvider;

public class FileDownloadProvider extends AbstractDownloadProvider {
  private File            file;

  private int              bufferSize      = DEF_BUFFER_SIZE;

  private String            contentType      = DEF_CONTENT_TYPE;

  private String            fileName;

  private static final String  DEF_CONTENT_TYPE  = "text/plain";

  private static final int    DEF_BUFFER_SIZE  = 4096;

  public FileDownloadProvider(File iFile, String iContentType) {
    file = iFile;
    contentType = iContentType;
  }

  public FileDownloadProvider(File iFile) {
    file = iFile;
  }

  /**
   * @see nextapp.echo2.app.filetransfer.ResourceDownloadProvider#writeFile(java.io.OutputStream)
   */
  public void writeFile(OutputStream out) throws IOException {
    InputStream in = null;
    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;

    try {
      in = new FileInputStream(file);
      if (in == null) {
        throw new IllegalArgumentException("Specified resource does not exist: " + getFileName() + ".");
      }
      do {
        bytesRead = in.read(buffer);
        if (bytesRead > 0) {
          out.write(buffer, 0, bytesRead);
        }
      } while (bytesRead > 0);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ex) {
        }
      }
    }
  }

  @Override
  public int getSize() {
    return (int) file.length();
  }

  @Override
  public String getFileName() {
    if (fileName != null)
      return fileName;
    else
      return file.getName();
  }
  
  public void setFileName(String iFileName){
    fileName = iFileName;
  }

  public String getContentType() {
    return contentType;
  }

  public int getBufferSize() {
    return bufferSize;
  }

  public void setBufferSize(int bufferSize) {
    this.bufferSize = bufferSize;
  }

  public void setContentType(String contentType) {
    this.contentType = contentType;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.