diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java index 63569a1..6c52336 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java @@ -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 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; diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java index b92cd7c..f63952f 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java @@ -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 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"; diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java index 7b4223a..14befdf 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java @@ -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 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) { diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java index e1b444a..1fb422a 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java @@ -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 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 diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUDGI.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUDGI.java index be5a226..eb60d11 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUDGI.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUDGI.java @@ -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 size) throws IOException { InputStream in = getGiPartitionTitle().getFileAsStream(content.getFilename(), getDiscReader(), fileOffsetBlock, titleKey); return in; } diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java index 622a2ba..4646b72 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java @@ -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 size) throws IOException { WoomyZipFile zipFile = getSharedWoomyZipFile(); ZipEntry entry = getWoomyInfo().getContentFiles().get(content.getFilename().toLowerCase()); if (entry == null) { diff --git a/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java b/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java index aa0f769..a2fd7d4 100644 --- a/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java +++ b/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java @@ -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 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 size) throws IOException { String URL = URL_BASE + "/" + url; - return getInputStream(URL, offset); + return getInputStream(URL, offset, size); } }