Files
Aria/SFtpComponent/src/main/java/com/arialyy/aria/sftp/AbsSFtpInfoTask.java
laoyuyu 50b265f22a sftp 下载实现
使用零拷贝技术优化合并分块的功能
2020-01-15 20:28:15 +08:00

88 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.ILoaderVisitor;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.UnsupportedEncodingException;
/**
* 进行登录获取session获取文件信息
*/
public abstract class AbsSFtpInfoTask<WP extends AbsTaskWrapper> implements IInfoTask {
protected String TAG = CommonUtil.getClassName(this);
protected Callback callback;
private WP wrapper;
private FtpTaskOption option;
public AbsSFtpInfoTask(WP wp) {
this.wrapper = wp;
this.option = (FtpTaskOption) wrapper.getTaskOption();
}
protected abstract void getFileInfo(Session session)
throws JSchException, UnsupportedEncodingException, SftpException;
@Override public void run() {
try {
FtpUrlEntity entity = option.getUrlEntity();
String key = CommonUtil.getStrMd5(entity.hostName + entity.port + entity.user + 0);
Session session = SFtpSessionManager.getInstance().getSession(key);
if (session == null) {
session = SFtpUtil.getInstance().getSession(entity, 0);
}
getFileInfo(session);
} catch (JSchException e) {
e.printStackTrace();
fail(false, null);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fail(false, null);
} catch (SftpException e) {
e.printStackTrace();
fail(false, null);
}
}
protected FtpTaskOption getOption() {
return option;
}
protected WP getWrapper() {
return wrapper;
}
protected void fail(boolean needRetry, BaseException e) {
callback.onFail(getWrapper().getEntity(), e, needRetry);
}
@Override public void setCallback(Callback callback) {
this.callback = callback;
}
@Override public void accept(ILoaderVisitor visitor) {
visitor.addComponent(this);
}
}