Skip to content

Commit 8fa1e61

Browse files
committed
Standardising to fileWriteInputStream (it was confused with outputStream)
1 parent d9148a4 commit 8fa1e61

5 files changed

Lines changed: 23 additions & 36 deletions

File tree

src/main/java/picoded/dstack/FileWorkspace.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.List;
1313
import java.util.Set;
1414

15+
import org.apache.commons.io.IOUtils;
16+
1517
/**
1618
* Represent a file storage backend for a workspace
1719
*
@@ -141,15 +143,15 @@ default void writeString(final String filepath, String content, String encoding)
141143
* You are expected to close, the stream on your own, to avoid memory leaks
142144
*
143145
* @param filePath in the workspace to extract
144-
* @return the file contents, null if file does not exists
146+
* @return the file contents as an input stream, null if file does not exists
145147
*/
146148
default InputStream readInputStream(final String filePath) {
147149
byte[] byteArr = readByteArray(filePath);
148150
return new ByteArrayInputStream(byteArr);
149151
}
150152

151153
/**
152-
* Writes an output stream to a file creating the file if it does not exist.
154+
* Reads an input stream, and writes it to a fil, creating the file if it does not exist.
153155
* the parent directories of the file will be created if they do not exist.
154156
*
155157
* Note that depending on the implementaiton, this may not be optimized,
@@ -158,22 +160,14 @@ default InputStream readInputStream(final String filePath) {
158160
* @param filepath in the workspace to extract
159161
* @param data the content to write to the file
160162
**/
161-
default void writeOutputStream(final String filepath, final OutputStream data) {
162-
163+
default void writeInputStream(final String filepath, final InputStream data) {
163164
// Converts it to bytearray respectively
164165
byte[] rawBytes = null;
165166
try {
166-
if (data instanceof ByteArrayOutputStream) {
167-
rawBytes = ((ByteArrayOutputStream) data).toByteArray();
168-
} else {
169-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
170-
buffer.writeTo(data);
171-
rawBytes = buffer.toByteArray();
172-
}
167+
rawBytes = IOUtils.toByteArray(data);
173168
} catch (IOException e) {
174169
throw new RuntimeException(e);
175170
}
176-
177171
// Does the bytearray writes
178172
writeByteArray(filepath, rawBytes);
179173
}

src/main/java/picoded/dstack/core/Core_FileWorkspace.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ public InputStream readInputStream(final String filepath) {
184184
* @param filepath in the workspace to extract
185185
* @param data the content to write to the file
186186
**/
187-
public void writeOutputStream(final String filepath, final OutputStream data) {
188-
main.backend_fileWriteOutputStream(_oid, normalizeFilePathString(filepath), data);
187+
public void writeInputStream(final String filepath, final InputStream data) {
188+
main.backend_fileWriteInputStream(_oid, normalizeFilePathString(filepath), data);
189189
}
190190

191191
// Read / write byteArray information

src/main/java/picoded/dstack/core/Core_FileWorkspaceMap.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.util.HashSet;
99
import java.util.Set;
10+
11+
import org.apache.commons.io.IOUtils;
12+
1013
import java.io.InputStream;
1114
import java.io.OutputStream;
1215
import java.io.ByteArrayInputStream;
@@ -201,8 +204,8 @@ public InputStream backend_fileReadInputStream(final String oid, final String fi
201204
* @param filepath to use for the workspace
202205
* @param data to write the file with
203206
**/
204-
public void backend_fileWriteOutputStream(final String oid, final String filepath,
205-
final OutputStream data) {
207+
public void backend_fileWriteInputStream(final String oid, final String filepath,
208+
final InputStream data) {
206209

207210
// forward the null, and let the error handling below settle it
208211
if (data == null) {
@@ -212,13 +215,7 @@ public void backend_fileWriteOutputStream(final String oid, final String filepat
212215
// Converts it to bytearray respectively
213216
byte[] rawBytes = null;
214217
try {
215-
if (data instanceof ByteArrayOutputStream) {
216-
rawBytes = ((ByteArrayOutputStream) data).toByteArray();
217-
} else {
218-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
219-
buffer.writeTo(data);
220-
rawBytes = buffer.toByteArray();
221-
}
218+
rawBytes = IOUtils.toByteArray(data);
222219
} catch (IOException e) {
223220
throw new RuntimeException(e);
224221
}

src/main/java/picoded/dstack/stack/Stack_FileWorkspaceMap.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.List;
1212
import java.util.Set;
1313

14+
import org.apache.commons.io.IOUtils;
15+
1416
/**
1517
* Stacked implementation of KeyValueMap data structure.
1618
*
@@ -218,13 +220,13 @@ public InputStream backend_fileReadInputStream(final String oid, final String fi
218220
* @param data to write the file with
219221
**/
220222
@Override
221-
public void backend_fileWriteOutputStream(final String oid, final String filepath,
222-
final OutputStream data) {
223+
public void backend_fileWriteInputStream(final String oid, final String filepath,
224+
final InputStream data) {
223225

224226
// Due to the behaviour of how the file data needs to be handled across multiple layers
225227
// we only use an optimized "readStream" call if the filesystem is a single stack layer
226228
if (dataLayers.length == 1) {
227-
dataLayers[0].backend_fileWriteOutputStream(oid, filepath, data);
229+
dataLayers[0].backend_fileWriteInputStream(oid, filepath, data);
228230
return;
229231
}
230232

@@ -239,13 +241,7 @@ public void backend_fileWriteOutputStream(final String oid, final String filepat
239241
// Converts it to bytearray respectively
240242
byte[] rawBytes = null;
241243
try {
242-
if (data instanceof ByteArrayOutputStream) {
243-
rawBytes = ((ByteArrayOutputStream) data).toByteArray();
244-
} else {
245-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
246-
buffer.writeTo(data);
247-
rawBytes = buffer.toByteArray();
248-
}
244+
rawBytes = IOUtils.toByteArray(data);
249245
} catch (IOException e) {
250246
throw new RuntimeException(e);
251247
}

src/test/java/picoded/dstack/struct/simple/StructSimple_FileWorkspaceMap_test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.Assert.assertNull;
88
import static org.junit.Assert.assertTrue;
99

10+
import java.io.ByteArrayInputStream;
1011
import java.io.ByteArrayOutputStream;
1112
import java.io.InputStream;
1213
import java.util.Arrays;
@@ -335,11 +336,10 @@ public void readNonExistenceFile() {
335336
@Test
336337
public void writeAndReadToFile_stream() throws Exception {
337338
// Output stream to use for content
338-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
339-
buffer.write("data to write".getBytes());
339+
ByteArrayInputStream buffer = new ByteArrayInputStream("data to write".getBytes());
340340

341341
FileWorkspace fileWorkspace = testObj.newEntry();
342-
fileWorkspace.writeOutputStream("testPath", buffer);
342+
fileWorkspace.writeInputStream("testPath", buffer);
343343
assertNotNull(testObj.get(fileWorkspace._oid()).readByteArray("testPath"));
344344

345345
InputStream readData = testObj.get(fileWorkspace._oid()).readInputStream("testPath");

0 commit comments

Comments
 (0)