|
4 | 4 | import picoded.core.conv.StringConv; |
5 | 5 |
|
6 | 6 | import java.io.File; |
| 7 | +import java.io.IOException; |
7 | 8 | import java.io.InputStream; |
| 9 | +import java.io.OutputStream; |
8 | 10 | import java.io.ByteArrayInputStream; |
| 11 | +import java.io.ByteArrayOutputStream; |
9 | 12 | import java.util.List; |
10 | 13 | import java.util.Set; |
11 | 14 |
|
| 15 | +import org.apache.commons.io.IOUtils; |
| 16 | + |
12 | 17 | /** |
13 | 18 | * Represent a file storage backend for a workspace |
14 | 19 | * |
@@ -133,16 +138,46 @@ default void writeString(final String filepath, String content, String encoding) |
133 | 138 | //-------------------------------------------------------------------------- |
134 | 139 |
|
135 | 140 | /** |
136 | | - * Get the input stream representation of a given filepath |
| 141 | + * Get the input stream representation of a given filepath. |
| 142 | + * |
| 143 | + * You are expected to close, the stream on your own, to avoid memory leaks |
137 | 144 | * |
138 | 145 | * @param filePath in the workspace to extract |
139 | | - * @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 |
140 | 147 | */ |
141 | 148 | default InputStream readInputStream(final String filePath) { |
142 | 149 | byte[] byteArr = readByteArray(filePath); |
143 | 150 | return new ByteArrayInputStream(byteArr); |
144 | 151 | } |
145 | 152 |
|
| 153 | + /** |
| 154 | + * Reads an input stream, and writes it to a fil, creating the file if it does not exist. |
| 155 | + * the parent directories of the file will be created if they do not exist. |
| 156 | + * |
| 157 | + * Note that depending on the implementaiton, this may not be optimized, |
| 158 | + * and may only return after the OutputStream is fully processedd. |
| 159 | + * |
| 160 | + * @param filepath in the workspace to extract |
| 161 | + * @param data the content to write to the file |
| 162 | + **/ |
| 163 | + default void writeInputStream(final String filepath, final InputStream data) { |
| 164 | + // Converts it to bytearray respectively |
| 165 | + byte[] rawBytes = null; |
| 166 | + try { |
| 167 | + rawBytes = IOUtils.toByteArray(data); |
| 168 | + } catch (IOException e) { |
| 169 | + throw new RuntimeException(e); |
| 170 | + } finally { |
| 171 | + try { |
| 172 | + data.close(); |
| 173 | + } catch (IOException e) { |
| 174 | + throw new RuntimeException(e); |
| 175 | + } |
| 176 | + } |
| 177 | + // Does the bytearray writes |
| 178 | + writeByteArray(filepath, rawBytes); |
| 179 | + } |
| 180 | + |
146 | 181 | // |
147 | 182 | // Folder Pathing support |
148 | 183 | //-------------------------------------------------------------------------- |
|
0 commit comments