mirror of
https://github.com/Rogiel/httpchannel
synced 2025-12-08 16:42:48 +00:00
Improve documentation and implement a new ServiceID class
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This file is part of seedbox <github.com/seedbox>.
|
||||
*
|
||||
* seedbox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* seedbox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.rogiel.httpchannel.service.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class InputStreamDownloadChannel implements DownloadChannel {
|
||||
private final ReadableByteChannel channel;
|
||||
|
||||
private final long length;
|
||||
private final String filename;
|
||||
|
||||
public InputStreamDownloadChannel(InputStream in, final long length,
|
||||
final String filename) {
|
||||
this.channel = Channels.newChannel(in);
|
||||
this.length = length;
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(ByteBuffer dst) throws IOException {
|
||||
return channel.read(dst);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return channel.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
channel.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilesize() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of seedbox <github.com/seedbox>.
|
||||
*
|
||||
* seedbox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* seedbox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.rogiel.httpchannel.service.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class LinkedUploadChannel implements UploadChannel {
|
||||
private WritableByteChannel channel;
|
||||
private final LinkedUploadChannelCloseCallback closeCallback;
|
||||
|
||||
private final long length;
|
||||
private final String filename;
|
||||
private String downloadLink;
|
||||
|
||||
private boolean open = true;
|
||||
|
||||
public LinkedUploadChannel(LinkedUploadChannelCloseCallback closeCallback,
|
||||
long filesize, String filename) {
|
||||
this.closeCallback = closeCallback;
|
||||
this.filename = filename;
|
||||
this.length = filesize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write(ByteBuffer src) throws IOException {
|
||||
if (channel == null)
|
||||
throw new IOException("Channel is not linked yet");
|
||||
return channel.write(src);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return (channel != null ? channel.isOpen() : true) && open;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
open = false;
|
||||
downloadLink = closeCallback.finish();
|
||||
}
|
||||
|
||||
public interface LinkedUploadChannelCloseCallback {
|
||||
String finish() throws IOException;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilesize() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadLink() {
|
||||
return downloadLink;
|
||||
}
|
||||
|
||||
public void linkChannel(WritableByteChannel channel) throws IOException {
|
||||
if(this.channel != null)
|
||||
throw new IOException("This channel is already linked.");
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public boolean isLinked() {
|
||||
return channel != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* This file is part of seedbox <github.com/seedbox>.
|
||||
*
|
||||
* seedbox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* seedbox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.rogiel.httpchannel.service.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
import org.apache.http.entity.mime.content.AbstractContentBody;
|
||||
import org.apache.http.entity.mime.content.ContentBody;
|
||||
|
||||
import com.rogiel.httpchannel.service.Uploader;
|
||||
|
||||
/**
|
||||
* {@link ContentBody} used to upload files in {@link Uploader} implementations.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class LinkedUploadChannelContentBody extends AbstractContentBody {
|
||||
private final LinkedUploadChannel channel;
|
||||
|
||||
public LinkedUploadChannelContentBody(LinkedUploadChannel channel) {
|
||||
super("application/octet-stream");
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return channel.getFilename();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
final WritableByteChannel outputChannel = Channels.newChannel(out);
|
||||
channel.linkChannel(outputChannel);
|
||||
|
||||
while (channel.isOpen() && outputChannel.isOpen()) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCharset() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return channel.getFilesize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTransferEncoding() {
|
||||
return "binary";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user