mirror of
https://github.com/Rogiel/httpchannel
synced 2025-12-05 23:22:51 +00:00
Implements SeekableDownloadChannel
The SeekableDownloadChannel implements SeekableByteChannel and allows to set the position of the channel and read data from that point onward. The SeekableDownloadChannel implementation, creates a new internal DownloadChannel at every call to position(long), that will create a resume channel onto that point, thus, not all service implementations are supported.
This commit is contained in:
@@ -22,6 +22,7 @@ import com.rogiel.httpchannel.service.exception.DownloadNotResumableException;
|
||||
*/
|
||||
public abstract class AbstractDownloader<C extends DownloaderConfiguration>
|
||||
implements Downloader<C> {
|
||||
protected final DownloadService<?> service;
|
||||
/**
|
||||
* The download URI
|
||||
*/
|
||||
@@ -35,12 +36,16 @@ public abstract class AbstractDownloader<C extends DownloaderConfiguration>
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param service
|
||||
* the download service
|
||||
* @param uri
|
||||
* the download uri
|
||||
* @param configuration
|
||||
* the configuration object
|
||||
*/
|
||||
protected AbstractDownloader(URI uri, C configuration) {
|
||||
protected AbstractDownloader(DownloadService<?> service, URI uri,
|
||||
C configuration) {
|
||||
this.service = service;
|
||||
this.uri = uri;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
@@ -69,7 +74,8 @@ public abstract class AbstractDownloader<C extends DownloaderConfiguration>
|
||||
|
||||
protected InputStreamDownloadChannel createInputStreamChannel(
|
||||
InputStream in, long length, String filename) {
|
||||
return new InputStreamDownloadChannel(in, length, filename);
|
||||
return new InputStreamDownloadChannel(service, this, in, length,
|
||||
filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -38,9 +38,10 @@ import com.rogiel.httpchannel.util.ThreadUtils;
|
||||
public abstract class AbstractHttpDownloader<C extends DownloaderConfiguration>
|
||||
extends AbstractDownloader<C> implements Downloader<C> {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected AbstractHttpDownloader(URI uri, C configuration) {
|
||||
super(uri, configuration);
|
||||
|
||||
protected AbstractHttpDownloader(DownloadService<?> service, URI uri,
|
||||
C configuration) {
|
||||
super(service, uri, configuration);
|
||||
}
|
||||
|
||||
protected long getContentLength(HttpResponse response) {
|
||||
|
||||
@@ -15,7 +15,17 @@ import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadCh
|
||||
*/
|
||||
public abstract class AbstractUploader<C extends UploaderConfiguration>
|
||||
implements Uploader<C> {
|
||||
/**
|
||||
* The upload service
|
||||
*/
|
||||
protected final UploadService<?> service;
|
||||
/**
|
||||
* The name of the file to be uploaded
|
||||
*/
|
||||
protected final String filename;
|
||||
/**
|
||||
* The size of the file to be uploaded
|
||||
*/
|
||||
protected final long filesize;
|
||||
|
||||
/**
|
||||
@@ -26,6 +36,8 @@ public abstract class AbstractUploader<C extends UploaderConfiguration>
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param service
|
||||
* the upload service
|
||||
* @param filename
|
||||
* the file name
|
||||
* @param filesize
|
||||
@@ -33,7 +45,9 @@ public abstract class AbstractUploader<C extends UploaderConfiguration>
|
||||
* @param configuration
|
||||
* the configuration object
|
||||
*/
|
||||
public AbstractUploader(String filename, long filesize, C configuration) {
|
||||
public AbstractUploader(UploadService<?> service, String filename,
|
||||
long filesize, C configuration) {
|
||||
this.service = service;
|
||||
this.filename = filename;
|
||||
this.filesize = filesize;
|
||||
this.configuration = configuration;
|
||||
@@ -48,7 +62,8 @@ public abstract class AbstractUploader<C extends UploaderConfiguration>
|
||||
*/
|
||||
protected LinkedUploadChannel createLinkedChannel(
|
||||
LinkedUploadChannelCloseCallback closeCallback) {
|
||||
return new LinkedUploadChannel(closeCallback, filesize, filename);
|
||||
return new LinkedUploadChannel(service, this, closeCallback, filesize,
|
||||
filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,8 @@ import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
|
||||
import com.rogiel.httpchannel.service.DownloadService;
|
||||
import com.rogiel.httpchannel.service.Downloader;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
@@ -32,11 +33,17 @@ import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
public class InputStreamDownloadChannel implements DownloadChannel {
|
||||
private final ReadableByteChannel channel;
|
||||
|
||||
private final DownloadService<?> service;
|
||||
private final Downloader<?> downloader;
|
||||
|
||||
private final long length;
|
||||
private final String filename;
|
||||
|
||||
public InputStreamDownloadChannel(InputStream in, final long length,
|
||||
public InputStreamDownloadChannel(DownloadService<?> service,
|
||||
Downloader<?> downloader, InputStream in, final long length,
|
||||
final String filename) {
|
||||
this.service = service;
|
||||
this.downloader = downloader;
|
||||
this.channel = Channels.newChannel(in);
|
||||
this.length = length;
|
||||
this.filename = filename;
|
||||
@@ -58,12 +65,22 @@ public class InputStreamDownloadChannel implements DownloadChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilesize() {
|
||||
public long size() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
public String filename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadService<?> getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Downloader<?> getDownloader() {
|
||||
return downloader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
import com.rogiel.httpchannel.service.UploadService;
|
||||
import com.rogiel.httpchannel.service.Uploader;
|
||||
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
|
||||
|
||||
/**
|
||||
@@ -41,6 +43,15 @@ public class LinkedUploadChannel implements UploadChannel {
|
||||
*/
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
/**
|
||||
* The upload service
|
||||
*/
|
||||
private final UploadService<?> service;
|
||||
/**
|
||||
* The uploader instance
|
||||
*/
|
||||
private final Uploader<?> uploader;
|
||||
|
||||
/**
|
||||
* The destionation {@link Channel}. Data writted is forwarded to this
|
||||
* channel.
|
||||
@@ -69,8 +80,11 @@ public class LinkedUploadChannel implements UploadChannel {
|
||||
*/
|
||||
private boolean open = true;
|
||||
|
||||
public LinkedUploadChannel(LinkedUploadChannelCloseCallback closeCallback,
|
||||
long filesize, String filename) {
|
||||
public LinkedUploadChannel(UploadService<?> service, Uploader<?> uploader,
|
||||
LinkedUploadChannelCloseCallback closeCallback, long filesize,
|
||||
String filename) {
|
||||
this.service = service;
|
||||
this.uploader = uploader;
|
||||
this.closeCallback = closeCallback;
|
||||
this.filename = filename;
|
||||
this.length = filesize;
|
||||
@@ -110,12 +124,12 @@ public class LinkedUploadChannel implements UploadChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilesize() {
|
||||
public long size() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
public String filename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
@@ -124,6 +138,16 @@ public class LinkedUploadChannel implements UploadChannel {
|
||||
return downloadLink;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadService<?> getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uploader<?> getUploader() {
|
||||
return uploader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Links this {@link Channel} to the destionation {@link Channel}. All data
|
||||
* written in this channel will be redirected to the destination
|
||||
|
||||
@@ -42,7 +42,7 @@ public class LinkedUploadChannelContentBody extends AbstractContentBody {
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return channel.getFilename();
|
||||
return channel.filename();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +64,7 @@ public class LinkedUploadChannelContentBody extends AbstractContentBody {
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return channel.getFilesize();
|
||||
return channel.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user