Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…/java/nio/file/Path.html#subpath-int-int- subpath shall return a relative path
  • Loading branch information
stefanofornari committed Mar 3, 2024
1 parent a197a93 commit 6f05941
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/main/java/software/amazon/nio/spi/s3/S3Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ public S3Path subpath(int beginIndex, int endIndex) {
}

var path = String.join(PATH_SEPARATOR, pathRepresentation.elements().subList(beginIndex, endIndex));
if (this.isAbsolute() && beginIndex == 0) {
path = PATH_SEPARATOR + path;
}

if (endIndex == size && !pathRepresentation.hasTrailingSeparator()) {
return from(path);
} else {
Expand Down Expand Up @@ -527,6 +523,7 @@ public S3Path resolveSibling(String other) {
* against this path
*/
@Override
/*
public S3Path relativize(Path other) {
var otherPath = checkPath(other);
Expand Down Expand Up @@ -569,6 +566,48 @@ public S3Path relativize(Path other) {
return new S3Path(getFileSystem(), new PosixLikePathRepresentation(relativePath));
}
*/
public S3Path relativize(Path other) {
var otherPath = checkPath(other);

if (this.equals(otherPath)) {
return from("");
}

if (this.isAbsolute() != otherPath.isAbsolute()) {
throw new IllegalArgumentException("to obtain a relative path both must be absolute or both must be relative");
}
if (!Objects.equals(this.bucketName(), otherPath.bucketName())) {
throw new IllegalArgumentException("cannot relativize S3Paths from different buckets");
}

if (this.isEmpty()) {
return otherPath;
}

int nameCount = this.getNameCount();
int otherNameCount = otherPath.getNameCount();

int limit = Math.min(nameCount, otherNameCount);
int differenceCount = getDifferenceCount(otherPath, limit);

int parentDirCount = nameCount - differenceCount;
if (differenceCount < otherNameCount) {
return getRelativePathFromDifference(otherPath, otherNameCount, differenceCount, parentDirCount);
}

char[] relativePath = new char[parentDirCount*3 - 1];
int index = 0;
while (parentDirCount > 0) {
relativePath[index++] = '.';
relativePath[index++] = '.';
if (parentDirCount > 1)
relativePath[index++] = '/';
parentDirCount--;
}

return new S3Path(getFileSystem(), new PosixLikePathRepresentation(relativePath));
}

private S3Path getRelativePathFromDifference(S3Path otherPath, int otherNameCount, int differenceCount, int parentDirCount) {
Objects.requireNonNull(otherPath);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/software/amazon/nio/spi/s3/S3PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ public void relativize() {
var bcd = fileSystem.getPath("b/c/d/");
var bcdObject = fileSystem.getPath("b/c/d/object");

assertFalse(root.relativize(ab).isAbsolute());

assertEquals(fileSystem.getPath(""), absoluteObject.relativize(absoluteObject));
assertEquals(fileSystem.getPath("../.."), abcd.relativize(ab));
assertEquals(fileSystem.getPath("e/"), abcd.relativize(abcde));
Expand Down

0 comments on commit 6f05941

Please sign in to comment.