Skip to content

Commit d6a9d04

Browse files
committed
Add putBytesToFile methods
1 parent 31e5942 commit d6a9d04

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/test/java/org/apache/datasketches/common/TestUtil.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
*/
4343
public final class TestUtil {
4444

45-
private static final String userDir = System.getProperty("user.dir");
46-
4745
/**
4846
* TestNG group constants
4947
*/
5048
public static final String GENERATE_JAVA_FILES = "generate_java_files";
5149
public static final String CHECK_CPP_FILES = "check_cpp_files";
5250
public static final String CHECK_GO_FILES = "check_go_files";
51+
public static final String CHECK_RUST_FILES = "check_rust_files";
5352
public static final String CHECK_CPP_HISTORICAL_FILES = "check_cpp_historical_files";
5453

5554
/**
@@ -67,34 +66,74 @@ public final class TestUtil {
6766
*/
6867
public static final Path goPath = Path.of(".", "serialization_test_data", "go_generated_files");
6968

69+
/**
70+
* The project relative Path for Rust serialized sketches to be tested by Java.
71+
*/
72+
public static final Path rustPath = Path.of(".", "serialization_test_data", "rust_generated_files");
73+
7074
/**
7175
* The project relative Path for /src/test/resources
7276
*/
7377
public static final Path resPath = Path.of(".","src","test","resources");
7478

7579

76-
//Get Resources
77-
78-
private static final int BUF_SIZE = 1 << 13;
79-
80-
public static byte[] getFileBytes(Path basePath, String fileName) throws RuntimeException {
80+
/**
81+
* Gets all the bytes of a file as a byte array.
82+
* If the file is missing, this writes a warning message to the console.
83+
* @param basePath the base directory path where the file is located
84+
* @param fileName the simple file name of the file
85+
* @return a byte array
86+
* @throws RuntimeException for IO errors, or if resolved path is not a file or not readable.
87+
*/
88+
public static byte[] getFileBytes(final Path basePath, final String fileName) throws RuntimeException {
8189
Objects.requireNonNull(basePath, "input parameter 'Path basePath' cannot be null.");
8290
Objects.requireNonNull(fileName, "input parameter 'String fileName' cannot be null.");
8391
Path path = Path.of(basePath.toString(), fileName);
84-
Path absPath = path.toAbsolutePath(); //for debugging
92+
Path absPath = path.toAbsolutePath(); //for error output
8593
byte[] bytes = new byte[0]; //or null
86-
if (Files.notExists(path)) {
94+
if (Files.notExists(path)) { //In this specific case, just issue warning.
8795
System.err.println("File disappeared or not found: " + absPath);
88-
return bytes; //or null
96+
return bytes; //empty
8997
}
9098
if (!Files.isRegularFile(path) || !Files.isReadable(path)) {
9199
throw new RuntimeException("Path is not a regular file or not readable: " + absPath);
92100
}
93101
try {
94-
bytes = Files.readAllBytes(path);
95-
return bytes;
96-
} catch (IOException e) {
97-
throw new RuntimeException("System Error reading file: " + absPath + " " + e);
98-
}
99-
}
102+
bytes = Files.readAllBytes(path);
103+
return bytes;
104+
} catch (IOException e) {
105+
throw new RuntimeException("System IO Error reading file: " + absPath + " " + e);
106+
}
107+
}
108+
109+
/**
110+
* Puts all the bytes of the given byte array to a file with the given fileName.
111+
* This assumes that the base directory path is {@link #javaPath javaPath}.
112+
* @param fileName the name of the target file
113+
* @param bytes the given byte array
114+
*/
115+
public static void putBytesToJavaPathFile(final String fileName, final byte[] bytes) {
116+
putBytesToFile(javaPath, fileName, bytes);
117+
}
118+
119+
/**
120+
* Puts all the bytes of the given byte array to a basePath file with the given fileName.
121+
* @param basePath the directory path for the given fileName
122+
* @param fileName the name of the target file
123+
* @param bytes the given byte array
124+
* @throws RuntimeException for IO errors,
125+
*/
126+
public static void putBytesToFile(final Path basePath, final String fileName, final byte[] bytes) {
127+
Objects.requireNonNull(basePath, "input parameter 'Path basePath' cannot be null.");
128+
Objects.requireNonNull(fileName, "input parameter 'String fileName' cannot be null.");
129+
Objects.requireNonNull(bytes, "input parameter 'byte[] bytes' cannot be null.");
130+
Path filePath = null;
131+
try {
132+
Files.createDirectories(basePath); //create the directory if it doesn't exist.
133+
filePath = basePath.resolve(fileName);
134+
Files.write(filePath, bytes);
135+
} catch (IOException e) {
136+
throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);
137+
}
138+
}
100139
}

src/test/java/org/apache/datasketches/common/TestUtilTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testGetFileBytes_Success() throws IOException {
4444
byte[] resultBytes = getFileBytes(resPath, "GettysburgAddress.txt");
4545
assertNotNull(resultBytes);
4646
String resultString = new String(resultBytes, UTF_8);
47-
assertTrue(resultString.startsWith("Abraham Lincoln's Gettysburg Address:"));
47+
assertTrue(resultString.startsWith("Abraham Lincoln's Gettysburg Address:"));
4848
}
4949

5050
@Test
@@ -63,4 +63,7 @@ public void testGetFileBytes_NotRegular_NotReadable() throws IOException {
6363
}
6464
}
6565

66+
67+
68+
6669
}

0 commit comments

Comments
 (0)