Android Open Source - prim-ftpd Android Ftp File






From Project

Back to project page prim-ftpd.

License

The source code is released under:

Apache License

If you think the Android project prim-ftpd 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 org.primftpd.filesystem;
//  ww w.j a va  2  s .  c o m
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import org.apache.ftpserver.ftplet.FtpFile;
import org.apache.ftpserver.ftplet.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AndroidFtpFile implements FtpFile {

  private static final Logger logger = LoggerFactory.getLogger(AndroidFtpFile.class);

  private final File file;
  private final User user;

  public AndroidFtpFile(File file, User user) {
    super();
    this.file = file;
    this.user = user;
  }

  @Override
  public String getAbsolutePath() {
    logger.debug("getAbsolutePath()");
    return file.getAbsolutePath();
  }

  @Override
  public String getName() {
    logger.debug("getName()");
    return file.getName();
  }

  @Override
  public boolean isHidden() {
    logger.debug("isHidden()");
    return file.isHidden();
  }

  @Override
  public boolean isDirectory() {
    logger.debug("isDirectory()");
    return file.isDirectory();
  }

  @Override
  public boolean isFile() {
    logger.debug("isFile()");
    return file.isFile();
  }

  @Override
  public boolean doesExist() {
    logger.debug("doesExist(), ({})", file.getAbsolutePath());
    return file.exists();
  }

  @Override
  public boolean isReadable() {
    logger.debug("isReadable()");
    return file.canRead();
  }

  @Override
  public boolean isWritable() {
    logger.debug(
      "writable: {}, exists: {}, file: '{}'",
      new Object[]{
        file.canWrite(),
        file.exists(),
        file.getName()
      });

    if (file.exists()) {
      return file.canWrite();
    }

    // file does not exist, probably an upload of a new file, check parent
    // must be done in loop as some clients to not issue mkdir commands
    // like filezilla
    File parent = file.getParentFile();
    while (parent != null) {
      if (parent.exists()) {
        return parent.canWrite();
      }
      parent = parent.getParentFile();
    }
    return false;
  }

  @Override
  public boolean isRemovable() {
    logger.debug("isRemovable()");
    return file.canWrite();
  }

  @Override
  public String getOwnerName() {
    logger.debug("getOwnerName()");
    return user.getName();
  }

  @Override
  public String getGroupName() {
    logger.debug("getGroupName()");
    return user.getName();
  }

  @Override
  public int getLinkCount() {
    logger.debug("getLinkCount()");
    return 0;
  }

  @Override
  public long getLastModified() {
    logger.debug("getLastModified()");
    return file.lastModified();
  }

  @Override
  public boolean setLastModified(long time) {
    logger.debug("setLastModified({})", time);
    return file.setLastModified(time);
  }

  @Override
  public long getSize() {
    logger.debug("getSize()");
    return file.length();
  }

  @Override
  public boolean mkdir() {
    logger.debug("mkdir()");
    return file.mkdir();
  }

  @Override
  public boolean delete() {
    logger.debug("delete()");
    return file.delete();
  }

  @Override
  public boolean move(FtpFile destination) {
    logger.debug("move({})", destination.getAbsolutePath());
    file.renameTo(new File(destination.getAbsolutePath()));
    return true;
  }

  @Override
  public List<FtpFile> listFiles() {
    logger.debug("listFiles()");
    File[] filesArray = file.listFiles();
    if (filesArray != null) {
      List<FtpFile> files = new ArrayList<FtpFile>(filesArray.length);
      for (File file : filesArray) {
        files.add(new AndroidFtpFile(file, user));
      }
      return files;
    }
    logger.debug("file.listFiles() returned null. Path: {}", file.getAbsolutePath());
    return new ArrayList<FtpFile>(0);
  }

  public static final int BUFFER_SIZE = 1024 * 1024;

  @Override
  public OutputStream createOutputStream(long offset) throws IOException {
    logger.debug("createOutputStream({})", offset);

    // may be necessary to create dirs
    // see isWritable()
    File parent = file.getParentFile();
    if (!parent.exists()) {
      parent.mkdirs();
    }

    // now create out stream
    OutputStream os = null;
    if (offset == 0) {
      os = new FileOutputStream(file);
    } else if (offset == this.file.length()) {
      os = new FileOutputStream(file, true);
    } else {
      final RandomAccessFile raf = new RandomAccessFile(this.file, "rw");
      raf.seek(offset);
      os = new OutputStream() {
        @Override
        public void write(int oneByte) throws IOException {
          raf.write(oneByte);
        }
        @Override
        public void close() throws IOException {
          raf.close();
        }
      };
    }

    BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE);
    return bos;
  }

  @Override
  public InputStream createInputStream(long offset) throws IOException {
    logger.debug("createInputStream(), offset: {}, file: {}", offset, file.getAbsolutePath());
    FileInputStream fis = new FileInputStream(file);
    fis.skip(offset);
    BufferedInputStream bis = new BufferedInputStream(fis, BUFFER_SIZE);
    return bis;
  }

  public File getFile() {
    return file;
  }

  public User getUser() {
    return user;
  }

}




Java Source Code List

org.primftpd.AndroidPrefsUserManager.java
org.primftpd.EncryptingEditTextPreference.java
org.primftpd.FtpPrefsActivity.java
org.primftpd.FtpPrefsFragment.java
org.primftpd.FtpServerService.java
org.primftpd.NotificationUtil.java
org.primftpd.PrefsBean.java
org.primftpd.PrimitiveFtpdActivity.java
org.primftpd.filesystem.AndroidFileSystemFactory.java
org.primftpd.filesystem.AndroidFileSystemView.java
org.primftpd.filesystem.AndroidFtpFile.java
org.primftpd.util.EncryptionUtil.java
org.primftpd.util.KeyStoreUtil.java
org.primftpd.util.StringUtils.java
org.slf4j.ILoggerFactory.java
org.slf4j.IMarkerFactory.java
org.slf4j.LoggerFactory.java
org.slf4j.Logger.java
org.slf4j.MDC.java
org.slf4j.MarkerFactory.java
org.slf4j.Marker.java
org.slf4j.helpers.BasicMDCAdapter.java
org.slf4j.helpers.BasicMarkerFactory.java
org.slf4j.helpers.BasicMarker.java
org.slf4j.helpers.FormattingTuple.java
org.slf4j.helpers.MarkerIgnoringBase.java
org.slf4j.helpers.MessageFormatter.java
org.slf4j.helpers.NOPLoggerFactory.java
org.slf4j.helpers.NOPLogger.java
org.slf4j.helpers.NOPMDCAdapter.java
org.slf4j.helpers.NamedLoggerBase.java
org.slf4j.helpers.SubstituteLoggerFactory.java
org.slf4j.helpers.Util.java
org.slf4j.impl.AndroidLoggerFactory.java
org.slf4j.impl.AndroidLogger.java
org.slf4j.impl.StaticLoggerBinder.java
org.slf4j.impl.StaticMDCBinder.java
org.slf4j.impl.StaticMarkerBinder.java
org.slf4j.spi.LocationAwareLogger.java
org.slf4j.spi.LoggerFactoryBinder.java
org.slf4j.spi.MDCAdapter.java
org.slf4j.spi.MarkerFactoryBinder.java