Skip to content

Commit

Permalink
Moved File->URI methods to FileUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
malhotrashivam committed Mar 13, 2024
1 parent 7188aa3 commit 12edf83
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 75 deletions.
71 changes: 71 additions & 0 deletions Base/src/main/java/io/deephaven/base/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.ArrayList;

public class FileUtils {
Expand Down Expand Up @@ -249,4 +252,72 @@ public boolean accept(File pathname) {
|| (pathname.isFile() && (normalFileFilter == null || normalFileFilter.accept(pathname)));
}
}

/**
* Take the file source path or URI string and convert it to a URI object.
*
* @param source The file source path or URI
* @param isDirectory Whether the source is a directory
* @return The URI object
*/
public static URI convertToURI(final String source, final boolean isDirectory) {
if (source.isEmpty()) {
throw new IllegalArgumentException("Cannot convert empty source to URI");
}
final URI uri;
try {
uri = new URI(source);
} catch (final URISyntaxException e) {
// If the URI is invalid, assume it's a file path
return convertToURI(new File(source), isDirectory);
}
if (uri.getScheme() == null) {
// Convert to a "file" URI
return convertToURI(new File(source), isDirectory);
}
return uri;
}

/**
* Takes a file and convert it to a URI object with {@code "file"} scheme. This method is preferred instead of
* {@link File#toURI()} because {@link File#toURI()} internally calls {@link File#isDirectory()}, which typically
* invokes the {@code stat} system call, resulting in filesystem metadata access.
*
* @param file The file
* @param isDirectory Whether the source file is a directory
* @return The URI object
*/
public static URI convertToURI(final File file, final boolean isDirectory) {
String absPath = file.getAbsolutePath();
if (File.separatorChar != '/') {
absPath = absPath.replace(File.separatorChar, '/');
}
if (absPath.charAt(0) != '/') {
absPath = "/" + absPath;
}
if (isDirectory && absPath.charAt(absPath.length() - 1) != '/') {
absPath = absPath + "/";
}
if (absPath.startsWith("//")) {
absPath = "//" + absPath;
}
try {
return new URI("file", null, absPath, null);
} catch (final URISyntaxException e) {
throw new IllegalStateException("Failed to convert file to URI: " + file, e);
}
}

/**
* Takes a path and convert it to a URI object with {@code "file"} scheme. This method is preferred instead of
* {@link Path#toUri()} because {@link Path#toUri()} internally invokes the {@code stat} system call, resulting in
* filesystem metadata access.
*
* @param path The path
* @param isDirectory Whether the file is a directory
* @return The URI object
*/
public static URI convertToURI(final Path path, final boolean isDirectory) {
return convertToURI(path.toFile(), isDirectory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,16 @@
import io.deephaven.util.SafeCloseable;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

public interface SeekableChannelsProvider extends SafeCloseable {

/**
* Take the file source path or URI string and convert it to a URI object.
*
* @param source The file source path or URI
* @param isDirectory Whether the source is a directory
* @return The URI object
*/
static URI convertToURI(final String source, final boolean isDirectory) {
if (source.isEmpty()) {
throw new IllegalArgumentException("Cannot convert empty source to URI");
}
final URI uri;
try {
uri = new URI(source);
} catch (final URISyntaxException e) {
// If the URI is invalid, assume it's a file path
return convertToURI(new File(source), isDirectory);
}
if (uri.getScheme() == null) {
// Convert to a "file" URI
return convertToURI(new File(source), isDirectory);
}
return uri;
}

/**
* Takes a file and convert it to a URI object. This method is preferred instead of {@link File#toURI()} because
* {@link File#toURI()} internally calls {@link File#isDirectory()}, which typically invokes the {@code stat} system
* call, resulting in filesystem metadata access.
*
* @param file The file
* @param isDirectory Whether the source file is a directory
* @return The URI object
*/
static URI convertToURI(final File file, final boolean isDirectory) {
String absPath = file.getAbsolutePath();
if (File.separatorChar != '/') {
absPath = absPath.replace(File.separatorChar, '/');
}
if (absPath.charAt(0) != '/') {
absPath = "/" + absPath;
}
if (isDirectory && absPath.charAt(absPath.length() - 1) != '/') {
absPath = absPath + "/";
}
if (absPath.startsWith("//")) {
absPath = "//" + absPath;
}
try {
return new URI("file", null, absPath, null);
} catch (final URISyntaxException e) {
throw new IllegalStateException("Failed to convert file to URI: " + file, e);
}
}
import static io.deephaven.base.FileUtils.convertToURI;

/**
* Takes a path and convert it to a URI object. This method is preferred instead of {@link Path#toUri()} because
* {@link Path#toUri()} internally invokes the {@code stat} system call, resulting in filesystem metadata access.
*
* @param path The path
* @param isDirectory Whether the file is a directory
* @return The URI object
*/
static URI convertToURI(final Path path, final boolean isDirectory) {
return convertToURI(path.toFile(), isDirectory);
}
public interface SeekableChannelsProvider extends SafeCloseable {

/**
* Wraps {@link SeekableChannelsProvider#getInputStream(SeekableByteChannel)} to ensure the channel's position is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.util.NoSuchElementException;
import java.util.function.Function;

import static io.deephaven.base.FileUtils.convertToURI;
import static io.deephaven.parquet.base.ParquetFileReader.FILE_URI_SCHEME;
import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static org.apache.parquet.format.Encoding.PLAIN_DICTIONARY;
import static org.apache.parquet.format.Encoding.RLE_DICTIONARY;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.nio.charset.StandardCharsets;
import java.util.*;

import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static io.deephaven.base.FileUtils.convertToURI;

/**
* Top level accessor for a parquet file which can read both from a file path string or a CLI style file URI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import java.nio.file.Paths;
import java.util.*;

import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static io.deephaven.base.FileUtils.convertToURI;

/**
* API for writing DH tables in parquet format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

import static io.deephaven.base.FileUtils.convertToURI;
import static io.deephaven.parquet.base.ParquetFileReader.FILE_URI_SCHEME;
import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static io.deephaven.parquet.table.ParquetTableWriter.PARQUET_FILE_EXTENSION;
import static io.deephaven.util.type.TypeUtils.getUnboxedTypeIfBoxed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Map;
import java.util.stream.IntStream;

import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static io.deephaven.base.FileUtils.convertToURI;

/**
* {@link TableLocationKey} implementation for use with data stored in the parquet format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

import javax.annotation.Nullable;

import static io.deephaven.base.FileUtils.convertToURI;
import static io.deephaven.engine.testutil.TstUtils.assertTableEquals;
import static io.deephaven.engine.util.TableTools.booleanCol;
import static io.deephaven.engine.util.TableTools.byteCol;
Expand All @@ -108,7 +109,6 @@
import static io.deephaven.parquet.table.ParquetTools.readTable;
import static io.deephaven.parquet.table.ParquetTools.writeTable;
import static io.deephaven.util.QueryConstants.*;
import static io.deephaven.util.channel.SeekableChannelsProvider.convertToURI;
import static org.junit.Assert.*;

@Category(OutOfBandTest.class)
Expand Down

0 comments on commit 12edf83

Please sign in to comment.