Search in sources :

Example 86 with UnsynchronizedByteArrayInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 87 with UnsynchronizedByteArrayInputStream

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));
}
Also used : DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database) VariableByteOutputStream(org.exist.storage.io.VariableByteOutputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) VariableByteInputStream(org.exist.storage.io.VariableByteInputStream) Test(org.junit.Test)

Example 88 with UnsynchronizedByteArrayInputStream

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();
    }
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) AtomicValue(org.exist.xquery.value.AtomicValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) StringValue(org.exist.xquery.value.StringValue)

Example 89 with UnsynchronizedByteArrayInputStream

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();
    }
}
Also used : DLN(org.exist.numbering.DLN) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) ValueSequence(org.exist.xquery.value.ValueSequence) NodeId(org.exist.numbering.NodeId) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringValue(org.exist.xquery.value.StringValue)

Example 90 with UnsynchronizedByteArrayInputStream

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);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Element(org.w3c.dom.Element) Charset(java.nio.charset.Charset) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Aggregations

UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)114 InputStream (java.io.InputStream)102 Test (org.junit.Test)93 MarkShieldInputStream (org.apache.commons.io.input.MarkShieldInputStream)31 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)10 IOException (java.io.IOException)8 FilterInputStream (java.io.FilterInputStream)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)7 XMLResource (org.xmldb.api.modules.XMLResource)6 DBBroker (org.exist.storage.DBBroker)5 Txn (org.exist.storage.txn.Txn)5 Element (org.w3c.dom.Element)4 Collection (org.xmldb.api.base.Collection)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3