Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;
Expand Down Expand Up @@ -76,8 +77,10 @@ public DecodeQualification getDecodeQualification(Object productInputFile) {
if (fileExtension != null) {
boolean extensionMatches = Arrays.stream(TIFF_FILE_EXTENSION).anyMatch(fileExtension::equalsIgnoreCase);
if (extensionMatches) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(productInputFile)) {
return getDecodeQualificationImpl(imageInputStream);
try (InputStream fileInputStream = productPath.getFileSystem().provider().newInputStream(productPath, StandardOpenOption.READ)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(fileInputStream)) {
return getDecodeQualificationImpl(imageInputStream);
}
}
} else if (fileExtension.equalsIgnoreCase(ZIP_FILE_EXTENSION)) {
return checkZipArchive(productPath);
Expand Down
32 changes: 18 additions & 14 deletions snap-geotiff/src/main/java/org/esa/snap/dataio/geotiff/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.ByteOrder;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -57,22 +59,24 @@ public class Utils {
* @param filePath The file path
*/
public static boolean isCOGGeoTIFF(Path filePath) throws Exception {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(filePath.toFile())) {
final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
TIFFImageReader tiffReader = null;
while (imageReaders.hasNext()) {
final ImageReader reader = imageReaders.next();
if (reader instanceof TIFFImageReader) {
tiffReader = (TIFFImageReader) reader;
tiffReader.setInput(imageInputStream);
break;
try (InputStream fileInputStream = filePath.getFileSystem().provider().newInputStream(filePath, StandardOpenOption.READ)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(fileInputStream)) {
final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
TIFFImageReader tiffReader = null;
while (imageReaders.hasNext()) {
final ImageReader reader = imageReaders.next();
if (reader instanceof TIFFImageReader) {
tiffReader = (TIFFImageReader) reader;
tiffReader.setInput(imageInputStream);
break;
}
}
if (tiffReader == null) {
throw new Exception("No tiff reader found!");
}
}
if (tiffReader == null) {
throw new Exception("No tiff reader found!");
}

return isCOGGeoTIFF(tiffReader);
return isCOGGeoTIFF(tiffReader);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ protected AbstractRemoteWalker(IRemoteConnectionBuilder remoteConnectionBuilder)
public BasicFileAttributes readBasicFileAttributes(VFSPath path) throws IOException {
// check if the address represents a directory
String address = path.buildURL().toString();
String fileSystemSeparator = path.getFileSystem().getSeparator();
String fileSystemRoot = path.getFileSystem().getRoot().getPath();
URL directoryURL = new URL(address + (address.endsWith(fileSystemSeparator) ? "" : fileSystemSeparator));
URL directoryURL = getDirectoryURL(path);
HttpURLConnection connection = this.remoteConnectionBuilder.buildConnection(fileSystemRoot, directoryURL, "GET", null);
try {
int responseCode = connection.getResponseCode();
Expand All @@ -46,6 +45,12 @@ public BasicFileAttributes readBasicFileAttributes(VFSPath path) throws IOExcept
return readFileAttributes(address, path.toString(), fileSystemRoot);
}

protected URL getDirectoryURL(VFSPath path) throws IOException{
final String address = path.buildURL().toString();
final String fileSystemSeparator = path.getFileSystem().getSeparator();
return new URL(address + (address.endsWith(fileSystemSeparator) ? "" : fileSystemSeparator));
}

private BasicFileAttributes readFileAttributes(String urlAddress, String filePath, String fileSystemRoot) throws IOException {
RegularFileMetadata regularFileMetadata = HttpUtils.readRegularFileMetadata(urlAddress, this.remoteConnectionBuilder, fileSystemRoot);
return VFSFileAttributes.newFile(filePath, regularFileMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public int read(ByteBuffer destinationBuffer) throws IOException {
int bytesToRead = destinationBuffer.remaining();
if (destinationBuffer.hasArray()) {
bytes = destinationBuffer.array();
offset = destinationBuffer.arrayOffset();
offset = destinationBuffer.position();
} else {
bytes = new byte[bytesToRead];
offset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class S3ResponseHandler extends DefaultHandler {
*/
private static final String NEXT_CONTINUATION_TOKEN_ELEMENT = "NextContinuationToken";

/**
* The name of XML element for NextContinuationToken, used on parsing VFS service response XML.
*/
private static final String NEXT_MARKER_ELEMENT = "NextMarker";

/**
* The name of XML element for IsTruncated, used on parsing VFS service response XML.
*/
Expand Down Expand Up @@ -195,6 +200,7 @@ public void characters(char[] ch, int start, int length) throws SAXException {
this.isTruncated = getBooleanValue(ch, start, length);
break;
case NEXT_CONTINUATION_TOKEN_ELEMENT:
case NEXT_MARKER_ELEMENT:
this.nextContinuationToken = getTextValue(ch, start, length);
break;
case PREFIX_ELEMENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public synchronized List<BasicFileAttributes> walk(VFSPath dir) throws IOExcepti
return items;
}

protected URL getDirectoryURL(VFSPath dir) throws IOException {
String dirPath = dir.toString();
String s3Prefix = buildPrefix(dirPath + (dirPath.endsWith("/") ? "" : "/"));
return new URL(buildS3URL(s3Prefix, ""));
}

private String buildPrefix(String prefix) {
prefix = prefix.replace(this.root, "");
prefix = prefix.replaceAll("^/", "");
Expand All @@ -143,7 +149,7 @@ private String buildS3URL(String prefix, String nextContinuationToken) throws IO
addParam(paramBase, "prefix", prefix);
addParam(paramBase, "delimiter", this.delimiter);
StringBuilder params = new StringBuilder(paramBase);
addParam(params, "continuation-token", nextContinuationToken);
addParam(params, "marker", nextContinuationToken);
String s3URL = this.address + (this.address.endsWith(this.delimiter) ? "" : this.delimiter) + currentBucket;
if (params.length() > 0) {
s3URL += "?" + params;
Expand Down