Search in sources :

Example 6 with IOExceptionWithCause

use of org.apache.commons.io.IOExceptionWithCause in project opennms by OpenNMS.

the class VmwareRequisitionUrlConnection method getInputStream.

/**
 * {@inheritDoc}
 * <p/>
 * Creates a ByteArrayInputStream implementation of InputStream of the XML
 * marshaled version of the Requisition class. Calling close on this stream
 * is safe.
 */
@Override
public InputStream getInputStream() throws IOException {
    InputStream stream = null;
    try {
        final Requisition existingRequisition = getExistingRequisition(importRequest.getForeignSource());
        importRequest.setExistingRequisition(existingRequisition);
        final VmwareImporter importer = new VmwareImporter(importRequest);
        stream = new ByteArrayInputStream(jaxBMarshal(importer.getRequisition()).getBytes());
    } catch (Throwable e) {
        logger.warn("Problem getting input stream: '{}'", e);
        throw new IOExceptionWithCause("Problem getting input stream: " + e, e);
    }
    return stream;
}
Also used : IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition)

Example 7 with IOExceptionWithCause

use of org.apache.commons.io.IOExceptionWithCause in project hive by apache.

the class ReplicationUtils method deserializeCommand.

/**
 * Command implements Writable, but that's not terribly easy to use compared
 * to String, even if it plugs in easily into the rest of Hadoop. Provide
 * utility methods to easily serialize and deserialize Commands
 *
 * deserializeCommand instantiates a concrete Command and initializes it,
 * given a base64 String representation of it.
 */
public static Command deserializeCommand(String s) throws IOException {
    DataInput dataInput = new DataInputStream(new ByteArrayInputStream(Base64.decodeBase64(s)));
    String clazz = (String) ReaderWriter.readDatum(dataInput);
    Command cmd;
    try {
        cmd = (Command) Class.forName(clazz).newInstance();
    } catch (Exception e) {
        throw new IOExceptionWithCause("Error instantiating class " + clazz, e);
    }
    cmd.readFields(dataInput);
    return cmd;
}
Also used : DataInput(java.io.DataInput) IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 8 with IOExceptionWithCause

use of org.apache.commons.io.IOExceptionWithCause in project jackrabbit by apache.

the class BundleReader method readPropertyEntry.

/**
     * Deserializes a <code>PropertyState</code> from the data input stream.
     *
     * @param id the property id for the new property entry
     * @return the property entry
     * @throws IOException if an I/O error occurs.
     */
private NodePropBundle.PropertyEntry readPropertyEntry(PropertyId id) throws IOException {
    NodePropBundle.PropertyEntry entry = new NodePropBundle.PropertyEntry(id);
    int count = 1;
    if (version >= BundleBinding.VERSION_3) {
        int b = in.readUnsignedByte();
        entry.setType(b & 0x0f);
        int len = b >>> 4;
        if (len != 0) {
            entry.setMultiValued(true);
            if (len == 0x0f) {
                count = readVarInt() + 0x0f - 1;
            } else {
                count = len - 1;
            }
        }
        entry.setModCount((short) readVarInt());
    } else {
        // type and modcount
        int type = in.readInt();
        entry.setModCount((short) ((type >> 16) & 0x0ffff));
        type &= 0x0ffff;
        entry.setType(type);
        // multiValued
        entry.setMultiValued(in.readBoolean());
        // definitionId
        in.readUTF();
        // count
        count = in.readInt();
    }
    // values
    InternalValue[] values = new InternalValue[count];
    String[] blobIds = new String[count];
    for (int i = 0; i < count; i++) {
        InternalValue val;
        int type = entry.getType();
        switch(type) {
            case PropertyType.BINARY:
                int size = in.readInt();
                if (size == BundleBinding.BINARY_IN_DATA_STORE) {
                    val = InternalValue.create(binding.dataStore, readString());
                } else if (size == BundleBinding.BINARY_IN_BLOB_STORE) {
                    blobIds[i] = readString();
                    try {
                        BLOBStore blobStore = binding.getBlobStore();
                        if (blobStore instanceof ResourceBasedBLOBStore) {
                            val = InternalValue.create(((ResourceBasedBLOBStore) blobStore).getResource(blobIds[i]));
                        } else {
                            val = InternalValue.create(blobStore.get(blobIds[i]));
                        }
                    } catch (IOException e) {
                        if (binding.errorHandling.ignoreMissingBlobs()) {
                            log.warn("Ignoring error while reading blob-resource: " + e);
                            val = InternalValue.create(new byte[0]);
                        } else {
                            throw e;
                        }
                    } catch (Exception e) {
                        throw new IOExceptionWithCause("Unable to create property value: " + e.toString(), e);
                    }
                } else {
                    // short values into memory
                    byte[] data = new byte[size];
                    in.readFully(data);
                    val = InternalValue.create(data);
                }
                break;
            case PropertyType.DOUBLE:
                val = InternalValue.create(in.readDouble());
                break;
            case PropertyType.DECIMAL:
                val = InternalValue.create(readDecimal());
                break;
            case PropertyType.LONG:
                if (version >= BundleBinding.VERSION_3) {
                    val = InternalValue.create(readVarLong());
                } else {
                    val = InternalValue.create(in.readLong());
                }
                break;
            case PropertyType.BOOLEAN:
                val = InternalValue.create(in.readBoolean());
                break;
            case PropertyType.NAME:
                val = InternalValue.create(readQName());
                break;
            case PropertyType.WEAKREFERENCE:
                val = InternalValue.create(readNodeId(), true);
                break;
            case PropertyType.REFERENCE:
                val = InternalValue.create(readNodeId(), false);
                break;
            case PropertyType.DATE:
                if (version >= BundleBinding.VERSION_3) {
                    val = InternalValue.create(readDate());
                    break;
                }
            // else fall through
            default:
                if (version >= BundleBinding.VERSION_3) {
                    val = InternalValue.valueOf(readString(), entry.getType());
                } else {
                    // because writeUTF(String) has a size limit of 64k,
                    // Strings are serialized as <length><byte[]>
                    int len = in.readInt();
                    byte[] bytes = new byte[len];
                    in.readFully(bytes);
                    String stringVal = new String(bytes, "UTF-8");
                    // https://issues.apache.org/jira/browse/JCR-3083
                    if (PropertyType.DATE == entry.getType()) {
                        val = InternalValue.createDate(stringVal);
                    } else {
                        val = InternalValue.valueOf(stringVal, entry.getType());
                    }
                }
        }
        values[i] = val;
    }
    entry.setValues(values);
    entry.setBlobIds(blobIds);
    return entry;
}
Also used : IOException(java.io.IOException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) IOException(java.io.IOException) IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause)

Example 9 with IOExceptionWithCause

use of org.apache.commons.io.IOExceptionWithCause in project jackrabbit by apache.

the class BundleWriter method writeSmallBinary.

/**
     * Write a small binary value and return the data.
     *
     * @param value the binary value
     * @param state the property state (for error messages)
     * @param i the index (for error messages)
     * @return the data
     * @throws IOException if the data could not be read
     */
private byte[] writeSmallBinary(InternalValue value, NodePropBundle.PropertyEntry state, int i) throws IOException {
    try {
        int size = (int) value.getLength();
        out.writeInt(size);
        byte[] data = new byte[size];
        DataInputStream in = new DataInputStream(value.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
        out.write(data, 0, data.length);
        return data;
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + value;
        log.error(msg, e);
        throw new IOExceptionWithCause(msg, e);
    }
}
Also used : IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException)

Example 10 with IOExceptionWithCause

use of org.apache.commons.io.IOExceptionWithCause in project tika by apache.

the class AbstractDBParser method getConnection.

/**
     * Override this for special configuration of the connection, such as limiting
     * the number of rows to be held in memory.
     *
     * @param stream   stream to use
     * @param metadata metadata that could be used in parameterizing the connection
     * @param context  parsecontext that could be used in parameterizing the connection
     * @return connection
     * @throws java.io.IOException
     * @throws org.apache.tika.exception.TikaException
     */
protected Connection getConnection(InputStream stream, Metadata metadata, ParseContext context) throws IOException, TikaException {
    String connectionString = getConnectionString(stream, metadata, context);
    Connection connection = null;
    try {
        Class.forName(getJDBCClassName());
    } catch (ClassNotFoundException e) {
        throw new TikaException(e.getMessage());
    }
    try {
        connection = DriverManager.getConnection(connectionString);
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    return connection;
}
Also used : IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) TikaException(org.apache.tika.exception.TikaException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Aggregations

IOExceptionWithCause (org.apache.commons.io.IOExceptionWithCause)14 IOException (java.io.IOException)10 SQLException (java.sql.SQLException)5 TikaException (org.apache.tika.exception.TikaException)5 SAXException (org.xml.sax.SAXException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 InputStream (java.io.InputStream)3 Connection (java.sql.Connection)2 RepositoryException (javax.jcr.RepositoryException)2 InternalValue (org.apache.jackrabbit.core.value.InternalValue)2 BufferedImage (java.awt.image.BufferedImage)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInput (java.io.DataInput)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 PDComplexFileSpecification (org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification)1 PDActionURI (org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)1