use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_pastEndOfStream.
@Test
public void readBytes_pastEndOfStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final String testString = "helloWorld";
final byte[] testData = testString.getBytes();
final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
final byte[] result = new byte[testData.length];
int read = cfis.read(result);
assertEquals(testData.length, read);
assertArrayEquals(testData, result);
final byte[] endOfStreamResult = new byte[testData.length];
read = cfis.read(endOfStreamResult);
assertEquals(-1, read);
assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, endOfStreamResult);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class LDAPRealmTest method setUpBeforeClass.
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try (final InputStream is = new UnsynchronizedByteArrayInputStream(config.getBytes(UTF_8))) {
Configuration config = Configurator.parse(is);
realm = new LDAPRealm(null, config);
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BlobStoreImplTest method addAndVerify.
private BlobId addAndVerify(final BlobStore blobStore, final Tuple2<byte[], MessageDigest> blob) throws IOException {
final Tuple2<BlobId, Long> actualBlob;
try (final InputStream is = new UnsynchronizedByteArrayInputStream(blob._1)) {
actualBlob = blobStore.add(null, is);
}
assertNotNull(actualBlob);
assertArrayEquals(blob._2.getValue(), actualBlob._1.getId());
return actualBlob._1;
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class XmlRpcTest method uploadCompressedAndDownload.
@Test
public void uploadCompressedAndDownload() throws IOException, XmlRpcException {
final XmlRpcClient xmlrpc = getClient();
final String resURI = XmldbURI.ROOT_COLLECTION_URI.append("test.bin").toString();
final Date now = new Date(System.currentTimeMillis());
final byte[] binary = generateBinary((int) (MAX_UPLOAD_CHUNK * 1.5));
// 1) upload
String uploadedFileName = null;
try (final InputStream is = new UnsynchronizedByteArrayInputStream(binary)) {
final byte[] chunk = new byte[MAX_UPLOAD_CHUNK];
int len;
while ((len = is.read(chunk)) > -1) {
final byte[] compressed = Compressor.compress(chunk, len);
final List<Object> params = new ArrayList<>();
if (uploadedFileName != null) {
params.add(uploadedFileName);
}
params.add(compressed);
params.add(len);
uploadedFileName = (String) xmlrpc.execute("uploadCompressed", params);
}
}
// set the properties of the uploaded file
final List<Object> paramsEx = new ArrayList<>();
paramsEx.add(uploadedFileName);
paramsEx.add(resURI);
paramsEx.add(Boolean.TRUE);
paramsEx.add("application/octet-stream");
paramsEx.add(Boolean.FALSE);
paramsEx.add(now);
paramsEx.add(now);
xmlrpc.execute("parseLocalExt", paramsEx);
// 2) download
final List<Object> params = new ArrayList<>();
params.add(resURI);
params.add(Collections.emptyMap());
Map table = (Map) xmlrpc.execute("getDocumentData", params);
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
long offset = (int) table.get("offset");
byte[] data = (byte[]) table.get("data");
os.write(data);
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(String.valueOf(offset));
table = (Map<?, ?>) xmlrpc.execute("getNextExtendedChunk", params);
offset = Long.valueOf((String) table.get("offset"));
data = (byte[]) table.get("data");
os.write(data);
}
data = os.toByteArray();
assertArrayEquals(binary, data);
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method filterFilter_withIncrementReferenceCount.
@Test
public void filterFilter_withIncrementReferenceCount() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis = binaryValue.getInputStream();
// create a filter over the first BinaryValue, and reference count increment
final InputStream fis1 = new BinaryValueFilteringInputStream(bvis, true);
final BinaryValue filteredBinaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis1);
// create a second filter over the first filter, and reference count increment
final InputStream fbvis = filteredBinaryValue1.getInputStream();
final InputStream fis2 = new BinaryValueFilteringInputStream(fbvis, true);
final BinaryValue filteredBinaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis2);
// we now destroy the second filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the filtered binary value or original binary value, as BinaryValueFilteringInputStream increased the reference count.
filteredBinaryValue2.close();
assertTrue(filteredBinaryValue2.isClosed());
assertFalse(filteredBinaryValue1.isClosed());
assertFalse(binaryValue.isClosed());
// we should still be able to read from the filtered binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
filteredBinaryValue1.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// we now destroy the first filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the original binary value, as BinaryValueFilteringInputStream increased the reference count.
fis2.close();
filteredBinaryValue1.close();
assertTrue(filteredBinaryValue1.isClosed());
// we should still be able to read from the origin binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
binaryValue.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// finally close the original binary value
bvis.close();
binaryValue.close();
assertTrue(binaryValue.isClosed());
} finally {
binaryValueManager.runCleanupTasks();
}
}
Aggregations