Added optional parameter to limit the size of a input stream for content

This commit is contained in:
Maschell 2019-04-07 14:57:41 +02:00
parent ac1d08645d
commit dd8f7bcb5f
7 changed files with 32 additions and 13 deletions

View File

@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Optional;
import de.mas.wiiu.jnus.NUSTitle;
import de.mas.wiiu.jnus.Settings;
@ -155,7 +156,7 @@ public abstract class NUSDataProvider {
}
public byte[] getChunkFromContent(Content content, long offset, int size) throws IOException {
return StreamUtils.getBytesFromStream(getInputStreamFromContent(content, offset), size);
return StreamUtils.getBytesFromStream(getInputStreamFromContent(content, offset, Optional.of((long)size)), size);
}
/**
@ -165,7 +166,11 @@ public abstract class NUSDataProvider {
* @return
* @throws IOException
*/
public abstract InputStream getInputStreamFromContent(Content content, long offset) throws IOException;
public abstract InputStream getInputStreamFromContent(Content content, long offset, Optional<Long> size) throws IOException;
public InputStream getInputStreamFromContent(Content content, long offset) throws IOException {
return getInputStreamFromContent(content, offset, Optional.empty());
}
// TODO: JavaDocs
public abstract byte[] getContentH3Hash(Content content) throws IOException;

View File

@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Optional;
import de.mas.wiiu.jnus.NUSTitle;
import de.mas.wiiu.jnus.Settings;
@ -45,7 +46,7 @@ public final class NUSDataProviderLocal extends NUSDataProvider {
}
@Override
public InputStream getInputStreamFromContent(Content content, long offset) throws IOException {
public InputStream getInputStreamFromContent(Content content, long offset, Optional<Long> size) throws IOException {
File filepath = FileUtils.getFileIgnoringFilenameCases(getLocalPath(), content.getFilename());
if (filepath == null || !filepath.exists()) {
String errormsg = "Couldn't open \"" + getLocalPath() + File.separator + content.getFilename() + "\", file does not exist";

View File

@ -19,6 +19,7 @@ package de.mas.wiiu.jnus.implementations;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import de.mas.wiiu.jnus.NUSTitle;
import de.mas.wiiu.jnus.Settings;
@ -39,9 +40,9 @@ public class NUSDataProviderRemote extends NUSDataProvider implements Paralleliz
}
@Override
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock) throws IOException {
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, Optional<Long> size) throws IOException {
NUSDownloadService downloadService = NUSDownloadService.getDefaultInstance();
return downloadService.getInputStreamForURL(getRemoteURL(content), fileOffsetBlock);
return downloadService.getInputStreamForURL(getRemoteURL(content), fileOffsetBlock, size);
}
private String getRemoteURL(Content content) {

View File

@ -18,6 +18,7 @@ package de.mas.wiiu.jnus.implementations;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import de.mas.wiiu.jnus.NUSTitle;
import de.mas.wiiu.jnus.Settings;
@ -61,10 +62,14 @@ public class NUSDataProviderWUD extends NUSDataProvider {
}
@Override
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock) throws IOException {
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, Optional<Long> size) throws IOException {
WUDDiscReader discReader = getDiscReader();
long offset = getOffsetInWUD(content) + fileOffsetBlock;
return discReader.readEncryptedToInputStream(offset, content.getEncryptedFileSize() - fileOffsetBlock);
long usedSize = content.getEncryptedFileSize() - fileOffsetBlock;
if(size.isPresent()) {
usedSize = size.get();
}
return discReader.readEncryptedToInputStream(offset, usedSize);
}
@Override

View File

@ -18,6 +18,7 @@ package de.mas.wiiu.jnus.implementations;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import de.mas.wiiu.jnus.NUSTitle;
import de.mas.wiiu.jnus.Settings;
@ -44,7 +45,7 @@ public class NUSDataProviderWUDGI extends NUSDataProvider {
}
@Override
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock) throws IOException {
public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, Optional<Long> size) throws IOException {
InputStream in = getGiPartitionTitle().getFileAsStream(content.getFilename(), getDiscReader(), fileOffsetBlock, titleKey);
return in;
}

View File

@ -19,6 +19,7 @@ package de.mas.wiiu.jnus.implementations;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
@ -44,7 +45,7 @@ public class NUSDataProviderWoomy extends NUSDataProvider {
}
@Override
public InputStream getInputStreamFromContent(@NonNull Content content, long fileOffsetBlock) throws IOException {
public InputStream getInputStreamFromContent(@NonNull Content content, long fileOffsetBlock, Optional<Long> size) throws IOException {
WoomyZipFile zipFile = getSharedWoomyZipFile();
ZipEntry entry = getWoomyInfo().getContentFiles().get(content.getFilename().toLowerCase());
if (entry == null) {

View File

@ -23,6 +23,7 @@ import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import de.mas.wiiu.jnus.Settings;
@ -82,11 +83,15 @@ public final class NUSDownloadService extends Downloader {
return downloadFileToByteArray(URL);
}
public InputStream getInputStream(String URL, long offset) throws IOException {
public InputStream getInputStream(String URL, long offset, Optional<Long> size) throws IOException {
URL url_obj = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url_obj.openConnection();
connection.setRequestProperty("User-Agent", Settings.USER_AGENT);
connection.setRequestProperty("Range", "bytes=" + offset + "-");
String sizeString = "";
if(size.isPresent()) {
sizeString = Long.toString(size.get());
}
connection.setRequestProperty("Range", "bytes=" + offset + "-" + sizeString);
try {
connection.connect();
} catch (Exception e) {
@ -96,9 +101,9 @@ public final class NUSDownloadService extends Downloader {
return connection.getInputStream();
}
public InputStream getInputStreamForURL(String url, long offset) throws IOException {
public InputStream getInputStreamForURL(String url, long offset, Optional<Long> size) throws IOException {
String URL = URL_BASE + "/" + url;
return getInputStream(URL, offset);
return getInputStream(URL, offset, size);
}
}