use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class Compressor method uncompress.
/**
* Uncompress the byte array using GZip compression.
*
* @param buf the data to uncompress.
* @param os the destination for the uncompressed data;
*
* @return the number of bytes uncompressed
*
* @throws IOException if an error occurs
*/
public static int uncompress(final byte[] buf, final OutputStream os) throws IOException {
int written = 0;
try (final UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(buf);
final InputStream gzis = new GZIPInputStream(bais)) {
final byte[] tmp = new byte[4096];
int read;
while ((read = gzis.read(tmp)) != -1) {
os.write(tmp, 0, read);
written += read;
}
}
return written;
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class SimpleACLPermissionTest method roundtrip_write_read.
@Test
public void roundtrip_write_read() throws PermissionDeniedException, IOException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(2);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(2);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(2);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(2);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
assertEquals(0, permission.getACECount());
final int userId1 = 1;
final int mode1 = ALL;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, userId1, mode1);
final int groupId2 = 2;
final int mode2 = Permission.READ;
permission.addGroupACE(ACE_ACCESS_TYPE.DENIED, groupId2, mode2);
final VariableByteOutputStream os = new VariableByteOutputStream();
// write the acl out
permission.write(os);
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
assertEquals(2, permission.getACECount());
assertEquals(ACE_ACCESS_TYPE.ALLOWED, permission.getACEAccessType(0));
assertEquals(userId1, permission.getACEId(0));
assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
assertEquals(mode1, permission.getACEMode(0));
assertEquals(ACE_ACCESS_TYPE.DENIED, permission.getACEAccessType(1));
assertEquals(groupId2, permission.getACEId(1));
assertEquals(ACE_TARGET.GROUP, permission.getACETarget(1));
assertEquals(mode2, permission.getACEMode(1));
// get the written acl data
final byte[] data = os.toByteArray();
// create a new permission instance
SimpleACLPermission permission2 = new SimpleACLPermission(mockSecurityManager);
// read the acl in
permission2.read(new VariableByteInputStream(new UnsynchronizedByteArrayInputStream(data)));
assertEquals(2, permission2.getACECount());
assertEquals(ACE_ACCESS_TYPE.ALLOWED, permission2.getACEAccessType(0));
assertEquals(userId1, permission2.getACEId(0));
assertEquals(ACE_TARGET.USER, permission2.getACETarget(0));
assertEquals(mode1, permission2.getACEMode(0));
assertEquals(ACE_ACCESS_TYPE.DENIED, permission2.getACEAccessType(1));
assertEquals(groupId2, permission2.getACEId(1));
assertEquals(ACE_TARGET.GROUP, permission2.getACETarget(1));
assertEquals(mode2, permission2.getACEMode(1));
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class GMLHSQLIndexWorker method getGeometricPropertyForNode.
@Override
protected AtomicValue getGeometricPropertyForNode(XQueryContext context, NodeProxy p, Connection conn, String propertyName) throws SQLException, XPathException {
PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + " FROM " + GMLHSQLIndex.TABLE_NAME + " WHERE DOCUMENT_URI = ? AND NODE_ID_UNITS = ? AND NODE_ID = ?");
ps.setString(1, p.getOwnerDocument().getURI().toString());
ps.setInt(2, p.getNodeId().units());
byte[] bytes = new byte[p.getNodeId().size()];
p.getNodeId().serialize(bytes, 0);
ps.setBytes(3, bytes);
ResultSet rs = null;
try {
rs = ps.executeQuery();
if (!rs.next())
// Nothing returned
return AtomicValue.EMPTY_VALUE;
AtomicValue result = null;
if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
result = new BooleanValue(rs.getBoolean(1));
} else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
result = new DoubleValue(rs.getDouble(1));
} else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
result = new StringValue(rs.getString(1));
} else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
result = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1)));
} else
throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
if (rs.next()) {
// Should be impossible
throw new SQLException("More than one geometry for node " + p);
}
return result;
} finally {
if (rs != null)
rs.close();
ps.close();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class GMLHSQLIndexWorker method getGeometricPropertyForNodes.
@Override
protected ValueSequence getGeometricPropertyForNodes(XQueryContext context, NodeSet contextSet, Connection conn, String propertyName) throws SQLException, XPathException {
// TODO : generate it in AbstractGMLJDBCIndexWorker
String docConstraint = "";
boolean refine_query_on_doc = false;
if (contextSet != null) {
if (contextSet.getDocumentSet().getDocumentCount() <= index.getMaxDocsInContextToRefineQuery()) {
DocumentImpl doc;
Iterator<DocumentImpl> it = contextSet.getDocumentSet().getDocumentIterator();
doc = it.next();
docConstraint = "(DOCUMENT_URI = '" + doc.getURI().toString() + "')";
while (it.hasNext()) {
doc = it.next();
docConstraint = docConstraint + " OR (DOCUMENT_URI = '" + doc.getURI().toString() + "')";
}
if (LOG.isDebugEnabled()) {
LOG.debug("Refine query on documents is enabled.");
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Refine query on documents is disabled.");
}
}
}
PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + ", DOCUMENT_URI, NODE_ID_UNITS, NODE_ID" + " FROM " + GMLHSQLIndex.TABLE_NAME + (refine_query_on_doc ? " WHERE " + docConstraint : ""));
ResultSet rs = null;
try {
rs = ps.executeQuery();
ValueSequence result;
if (contextSet == null)
result = new ValueSequence();
else
result = new ValueSequence(contextSet.getLength());
while (rs.next()) {
DocumentImpl doc = null;
try {
doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
} catch (PermissionDeniedException e) {
LOG.debug(e);
// Untested, but that is roughly what should be returned.
if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
result.add(AtomicValue.EMPTY_VALUE);
} else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
result.add(AtomicValue.EMPTY_VALUE);
} else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
result.add(AtomicValue.EMPTY_VALUE);
} else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
result.add(AtomicValue.EMPTY_VALUE);
} else
throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
// Ignore since the broker has no right on the document
continue;
}
if (contextSet.getDocumentSet().contains(doc.getDocId())) {
NodeId nodeId = new DLN(rs.getInt("NODE_ID_UNITS"), rs.getBytes("NODE_ID"), 0);
NodeProxy p = new NodeProxy(doc, nodeId);
// VirtualNodeSet when on the DESCENDANT_OR_SELF axis
if (contextSet.get(p) != null) {
if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
result.add(new BooleanValue(rs.getBoolean(1)));
} else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
result.add(new DoubleValue(rs.getDouble(1)));
} else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
result.add(new StringValue(rs.getString(1)));
} else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
result.add(BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1))));
} else
throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
}
}
}
return result;
} finally {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class AbstractCompressFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// are there some uri's to tar?
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
// use a hierarchy in the tar file?
boolean useHierarchy = args[1].effectiveBooleanValue();
// Get offset
String stripOffset = "";
if (args.length == 3) {
stripOffset = args[2].getStringValue();
}
// Get encoding
try {
final Charset encoding;
if ((args.length >= 4) && !args[3].isEmpty()) {
encoding = Charset.forName(args[3].getStringValue());
} else {
encoding = StandardCharsets.UTF_8;
}
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
OutputStream os = stream(baos, encoding)) {
// iterate through the argument sequence
for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
Item item = i.nextItem();
if (item instanceof Element) {
Element element = (Element) item;
compressElement(os, element, useHierarchy, stripOffset);
} else {
compressFromUri(os, ((AnyURIValue) item).toURI(), useHierarchy, stripOffset, "", null);
}
}
os.flush();
if (os instanceof DeflaterOutputStream) {
((DeflaterOutputStream) os).finish();
}
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(baos.toByteArray()));
}
} catch (final UnsupportedCharsetException | IOException e) {
throw new XPathException(this, e.getMessage(), e);
}
}
Aggregations