use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class RemoteCollection method uploadAndStore.
private void uploadAndStore(final Resource res) throws XMLDBException {
InputStream is = null;
String descString = "<unknown>";
try {
if (res instanceof RemoteBinaryResource) {
is = ((RemoteBinaryResource) res).getStreamContent();
descString = ((RemoteBinaryResource) res).getStreamSymbolicPath();
} else {
final Object content = res.getContent();
if (content instanceof File) {
final File file = (File) content;
try {
is = new BufferedInputStream(new FileInputStream(file));
} catch (final FileNotFoundException e) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "could not read resource from file " + file.getAbsolutePath(), e);
}
} else if (content instanceof InputSource) {
is = ((InputSource) content).getByteStream();
if (content instanceof EXistInputSource) {
descString = ((EXistInputSource) content).getSymbolicPath();
}
} else if (content instanceof String) {
// TODO(AR) we really should not allow String to be used here, as we loose the encoding info and default to UTF-8!
is = new UnsynchronizedByteArrayInputStream(((String) content).getBytes(UTF_8));
} else {
LOG.error("Unable to get content from {}", content);
}
}
final byte[] chunk;
if (res instanceof ExtendedResource) {
if (res instanceof AbstractRemoteResource) {
final long contentLen = ((AbstractRemoteResource) res).getContentLength();
if (contentLen != -1) {
// content length is known
chunk = new byte[(int) Math.min(contentLen, MAX_UPLOAD_CHUNK)];
} else {
chunk = new byte[MAX_UPLOAD_CHUNK];
}
} else {
final long streamLen = ((ExtendedResource) res).getStreamLength();
if (streamLen != -1) {
// stream length is known
chunk = new byte[(int) Math.min(streamLen, MAX_UPLOAD_CHUNK)];
} else {
chunk = new byte[MAX_UPLOAD_CHUNK];
}
}
} else {
chunk = new byte[MAX_UPLOAD_CHUNK];
}
try {
String fileName = null;
if (chunk.length > 0) {
int len;
while ((len = is.read(chunk)) > -1) {
final List<Object> params = new ArrayList<>();
if (fileName != null) {
params.add(fileName);
}
/*
Only compress the chunk if it is larger than 256 bytes,
otherwise the compression framing overhead results in a larger chunk
*/
if (len < 256) {
params.add(chunk);
params.add(len);
fileName = (String) execute("upload", params);
} else {
final byte[] compressed = Compressor.compress(chunk, len);
params.add(compressed);
params.add(len);
fileName = (String) execute("uploadCompressed", params);
}
}
}
if (fileName == null) {
// Zero length stream? Let's get a fileName!
final List<Object> params = new ArrayList<>();
params.add(new byte[0]);
params.add(0);
fileName = (String) execute("upload", params);
}
final List<Object> params = new ArrayList<>();
final List<Object> paramsEx = new ArrayList<>();
params.add(fileName);
paramsEx.add(fileName);
try {
final String resURI = getPathURI().append(XmldbURI.xmldbUriFor(res.getId())).toString();
params.add(resURI);
paramsEx.add(resURI);
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
}
params.add(Boolean.TRUE);
paramsEx.add(Boolean.TRUE);
if (res instanceof EXistResource) {
final EXistResource rxres = (EXistResource) res;
params.add(rxres.getMimeType());
paramsEx.add(rxres.getMimeType());
// This one is only for the new style!!!!
paramsEx.add((BinaryResource.RESOURCE_TYPE.equals(res.getResourceType())) ? Boolean.FALSE : Boolean.TRUE);
if (rxres.getCreationTime() != null) {
params.add(rxres.getCreationTime());
paramsEx.add(rxres.getCreationTime());
params.add(rxres.getLastModificationTime());
paramsEx.add(rxres.getLastModificationTime());
}
}
try {
execute("parseLocalExt", paramsEx);
} catch (final XMLDBException e) {
// Identifying old versions
final String excMsg = e.getCause().getMessage();
if (excMsg.contains("No such handler") || excMsg.contains("No method matching")) {
execute("parseLocal", params);
} else {
throw e;
}
}
} catch (final IOException e) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "failed to read resource from " + descString, e);
}
} finally {
if (is != null) {
try {
is.close();
} catch (final IOException ioe) {
LOG.warn(ioe.getMessage(), ioe);
}
}
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CollectionStoreTest method storeBinary.
private void storeBinary(final PreserveType preserveOnCopy) throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
final byte[] bin = TEST_BIN_DOC.getBytes(UTF_8);
try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
final int docId = broker.getNextResourceId(transaction);
final BinaryDocument binDoc = col.addBinaryResource(transaction, broker, new BinaryDocument(broker.getBrokerPool(), col, docId, TEST_BIN_DOC_URI), is, "text/plain", bin.length, null, null, preserveOnCopy);
assertNotNull(binDoc);
}
broker.saveCollection(transaction, col);
}
try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_BIN_DOC_URI, LockMode.READ_LOCK)) {
// NOTE: early release of collection lock inline with async locking
col.close();
if (lockedDoc != null) {
assertTrue(lockedDoc.getDocument() instanceof BinaryDocument);
final BinaryDocument doc = (BinaryDocument) lockedDoc.getDocument();
final Try<String, IOException> docContent = broker.withBinaryFile(transaction, doc, is -> Try.TaggedTryUnchecked(IOException.class, () -> new String(Files.readAllBytes(is), UTF_8)));
assertEquals(TEST_BIN_DOC, docContent.get());
}
}
}
transaction.commit();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readBytes.
@Test
public void readBytes() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final String testString = "helloWorld";
final byte[] testData = testString.getBytes();
InputStream is = new UnsynchronizedByteArrayInputStream(testData);
CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
// read the first 3 bytes
byte[] result = new byte[3];
int read = cfis.read(result);
assertEquals(3, read);
assertArrayEquals(subArray(testData, 3), result);
// mark position
cfis.mark(Integer.MAX_VALUE);
// read the next 3 bytes
result = new byte[3];
read = cfis.read(result);
assertEquals(3, read);
assertArrayEquals(subArray(testData, 3, 3), result);
// reset position to the mark
cfis.reset();
// attempt to reread the last 3 bytes from the mark (from the cache)
result = new byte[3];
read = cfis.read(result);
assertEquals(3, read);
assertArrayEquals(subArray(testData, 3, 3), result);
// read the next 2 bytes past the reset mark (past the cache, e.g. from src)
result = new byte[2];
read = cfis.read(result);
assertEquals(2, read);
assertArrayEquals(subArray(testData, 6, 2), result);
// reset position to the mark
cfis.reset();
// attempt to read the last 5 bytes (from the cache)
result = new byte[5];
read = cfis.read(result);
assertEquals(5, read);
assertArrayEquals(subArray(testData, 3, 5), result);
// mark position
cfis.mark(-1);
// read the next 2 bytes past the reset mark (past the cache, e.g. from src)
result = new byte[2];
read = cfis.read(result);
assertEquals(2, read);
assertArrayEquals(subArray(testData, 8, 2), result);
// reset position to the mark
cfis.reset();
// attempt to reread the last 2 bytes from the mark (from the cache)
result = new byte[2];
read = cfis.read(result);
assertEquals(2, read);
assertArrayEquals(subArray(testData, 8, 2), result);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readByte_allFromCache.
@Test
public void readByte_allFromCache() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final String testString = "hello";
final byte[] testData = testString.getBytes();
InputStream is = new UnsynchronizedByteArrayInputStream(testData);
CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
// mark the position
cfis.mark(Integer.MAX_VALUE);
// read the data
assertEquals(testData[0], cfis.read());
assertEquals(testData[1], cfis.read());
assertEquals(testData[2], cfis.read());
assertEquals(testData[3], cfis.read());
assertEquals(testData[4], cfis.read());
// reset position to the mark
cfis.reset();
// attempt to reread the data (from the cache)
assertEquals(testData[0], cfis.read());
assertEquals(testData[1], cfis.read());
assertEquals(testData[2], cfis.read());
assertEquals(testData[3], cfis.read());
assertEquals(testData[4], cfis.read());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method available_onEmptyStream.
@Test
public void available_onEmptyStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
InputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {});
CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
cfis.close();
assertEquals(0, cfis.available());
}
Aggregations