Skip to content

Commit 6d844c9

Browse files
Add convenience method to get a Path to a file whose name includes spaces (#1463)
Also add tests to make sure spaces are handled correctly (they are).
1 parent c268bb0 commit 6d844c9

3 files changed

Lines changed: 106 additions & 57 deletions

File tree

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.common.base.MoreObjects;
3434
import com.google.common.base.Throwables;
3535
import com.google.common.collect.AbstractIterator;
36+
import com.google.common.net.UrlEscapers;
3637
import com.google.common.primitives.Ints;
3738

3839
import java.io.BufferedInputStream;
@@ -210,6 +211,12 @@ public CloudStoragePath getPath(URI uri) {
210211
getFileSystem(CloudStorageUtil.stripPathFromUri(uri)), uri.getPath());
211212
}
212213

214+
/** Convenience method: replaces spaces with "%20", builds a URI, and calls getPath(uri). */
215+
public CloudStoragePath getPath(String uriInStringForm) {
216+
String escaped = UrlEscapers.urlFragmentEscaper().escape(uriInStringForm);
217+
return getPath(URI.create(escaped));
218+
}
219+
213220
@Override
214221
public SeekableByteChannel newByteChannel(
215222
Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {

0 commit comments

Comments
 (0)