Search in sources :

Example 36 with StreamCorruptedException

use of java.io.StreamCorruptedException in project Chronicle-Queue by OpenHFT.

the class SingleTableStore method acquireValueFor.

/**
 * {@inheritDoc}
 */
@Override
public synchronized LongValue acquireValueFor(CharSequence key) {
    // TODO Change to ThreadLocal values if performance is a problem.
    StringBuilder sb = Wires.acquireStringBuilder();
    mappedBytes.reserve();
    try {
        mappedBytes.readPosition(0);
        mappedBytes.readLimit(mappedBytes.realCapacity());
        while (mappedWire.readDataHeader()) {
            int header = mappedBytes.readInt();
            if (Wires.isNotComplete(header))
                break;
            long readPosition = mappedBytes.readPosition();
            int length = Wires.lengthOf(header);
            ValueIn valueIn = mappedWire.readEventName(sb);
            if (StringUtils.equalsCaseIgnore(key, sb)) {
                return valueIn.int64ForBinding(null);
            }
            mappedBytes.readPosition(readPosition + length);
        }
        // not found
        int safeLength = Maths.toUInt31(mappedBytes.realCapacity() - mappedBytes.readPosition());
        mappedBytes.writeLimit(mappedBytes.realCapacity());
        mappedBytes.writePosition(mappedBytes.readPosition());
        long pos = recovery.writeHeader(mappedWire, safeLength, timeoutMS, null, null);
        LongValue longValue = wireType.newLongReference().get();
        mappedWire.writeEventName(key).int64forBinding(Long.MIN_VALUE, longValue);
        mappedWire.updateHeader(pos, false);
        return longValue;
    } catch (StreamCorruptedException | EOFException e) {
        throw new IORuntimeException(e);
    } finally {
        mappedBytes.release();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) LongValue(net.openhft.chronicle.core.values.LongValue) EOFException(java.io.EOFException) StreamCorruptedException(java.io.StreamCorruptedException)

Example 37 with StreamCorruptedException

use of java.io.StreamCorruptedException in project datanucleus-rdbms by datanucleus.

the class OracleBlobRDBMSMapping method getObject.

/**
 * Returns the object to be loaded from the Orale BLOB.
 * @param rs the ResultSet from the query
 * @param param the index in the query
 * @return the object loaded as a byte[]
 * @throws NucleusDataStoreException Thrown if an error occurs in datastore communication
 */
public Object getObject(ResultSet rs, int param) {
    Object obj = null;
    try {
        Blob blob = rs.getBlob(param);
        if (!rs.wasNull()) {
            byte[] bytes = blob.getBytes(1, (int) blob.length());
            if (bytes.length < 1) {
                return null;
            }
            try {
                if (getJavaTypeMapping().isSerialised()) {
                    BlobImpl b = new BlobImpl(bytes);
                    obj = b.getObject();
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.BOOLEAN_ARRAY)) {
                    obj = TypeConversionHelper.getBooleanArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.BYTE_ARRAY)) {
                    obj = bytes;
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.CHAR_ARRAY)) {
                    obj = TypeConversionHelper.getCharArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_STRING)) {
                    obj = new String(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.DOUBLE_ARRAY)) {
                    obj = TypeConversionHelper.getDoubleArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.FLOAT_ARRAY)) {
                    obj = TypeConversionHelper.getFloatArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.INT_ARRAY)) {
                    obj = TypeConversionHelper.getIntArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.LONG_ARRAY)) {
                    obj = TypeConversionHelper.getLongArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.SHORT_ARRAY)) {
                    obj = TypeConversionHelper.getShortArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_BOOLEAN_ARRAY)) {
                    obj = TypeConversionHelper.getBooleanObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_BYTE_ARRAY)) {
                    obj = TypeConversionHelper.getByteObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_CHARACTER_ARRAY)) {
                    obj = TypeConversionHelper.getCharObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_DOUBLE_ARRAY)) {
                    obj = TypeConversionHelper.getDoubleObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_FLOAT_ARRAY)) {
                    obj = TypeConversionHelper.getFloatObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_INTEGER_ARRAY)) {
                    obj = TypeConversionHelper.getIntObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_LONG_ARRAY)) {
                    obj = TypeConversionHelper.getLongObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(ClassNameConstants.JAVA_LANG_SHORT_ARRAY)) {
                    obj = TypeConversionHelper.getShortObjectArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(BigDecimal[].class.getName())) {
                    return TypeConversionHelper.getBigDecimalArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(BigInteger[].class.getName())) {
                    return TypeConversionHelper.getBigIntegerArrayFromByteArray(bytes);
                } else if (getJavaTypeMapping().getType().equals(java.util.BitSet.class.getName())) {
                    return TypeConversionHelper.getBitSetFromBooleanArray(TypeConversionHelper.getBooleanArrayFromByteArray(bytes));
                } else {
                    obj = new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
                }
            } catch (StreamCorruptedException e) {
                String msg = "StreamCorruptedException: object is corrupted";
                NucleusLogger.DATASTORE.error(msg);
                throw new NucleusUserException(msg, e).setFatal();
            } catch (IOException e) {
                String msg = "IOException: error when reading object";
                NucleusLogger.DATASTORE.error(msg);
                throw new NucleusUserException(msg, e).setFatal();
            } catch (ClassNotFoundException e) {
                String msg = "ClassNotFoundException: error when creating object";
                NucleusLogger.DATASTORE.error(msg);
                throw new NucleusUserException(msg, e).setFatal();
            }
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, sqle.getMessage()), sqle);
    }
    return obj;
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IOException(java.io.IOException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) StreamCorruptedException(java.io.StreamCorruptedException) ObjectInputStream(java.io.ObjectInputStream)

Example 38 with StreamCorruptedException

use of java.io.StreamCorruptedException in project derby by apache.

the class FormatIdInputStream method readObject.

/**
 *	  Read an object from this stream.
 *
 *	  @return The read object.
 *      @exception java.io.IOException An IO or serialization error occurred.
 *	  @exception java.lang.ClassNotFoundException A class for an object in
 *	  the stream could not be found.
 */
public Object readObject() throws IOException, ClassNotFoundException {
    setErrorInfo(null);
    int fmtId = FormatIdUtil.readFormatIdInteger(this);
    if (fmtId == StoredFormatIds.NULL_FORMAT_ID) {
        return null;
    }
    if (fmtId == StoredFormatIds.STRING_FORMAT_ID) {
        return readUTF();
    }
    try {
        if (fmtId == StoredFormatIds.SERIALIZABLE_FORMAT_ID) {
            ObjectInputStream ois = getObjectStream();
            try {
                Object result = ois.readObject();
                return result;
            } catch (IOException ioe) {
                throw handleReadError(ioe, ois);
            } catch (ClassNotFoundException cnfe) {
                throw handleReadError(cnfe, ois);
            } catch (LinkageError le) {
                throw handleReadError(le, ois);
            } catch (ClassCastException cce) {
                throw handleReadError(cce, ois);
            }
        }
        try {
            Formatable f = (Formatable) Monitor.newInstanceFromIdentifier(fmtId);
            if (f instanceof Storable) {
                boolean isNull = this.readBoolean();
                if (isNull == true) {
                    Storable s = (Storable) f;
                    s.restoreToNull();
                    return s;
                }
            }
            f.readExternal(this);
            return f;
        } catch (StandardException se) {
            throw new ClassNotFoundException(se.toString());
        }
    } catch (ClassCastException cce) {
        // We catch this here as it is usuall a user error.
        // they have readExternal (or SQLData) that doesn't match
        // the writeExternal. and thus the object read is of
        // the incorrect type, e.g. Integer i = (Integer) in.readObject();
        StreamCorruptedException sce = new StreamCorruptedException(cce.toString());
        sce.initCause(cce);
        throw sce;
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 39 with StreamCorruptedException

use of java.io.StreamCorruptedException in project TauP by crotwell.

the class TauModelLoader method internalLoad.

public static TauModel internalLoad(String modelName, String searchPath, boolean verbose) throws TauModelException {
    try {
        String filename;
        /* Append ".taup" to modelname if it isn't already there. */
        if (modelName.endsWith(".taup")) {
            filename = modelName;
        } else {
            filename = modelName + ".taup";
        }
        String classPath = System.getProperty("java.class.path");
        String taupPath = searchPath;
        int offset = 0;
        int pathSepIndex;
        String pathEntry;
        File jarFile;
        File modelFile;
        /* First we try to find the model in the distributed taup.jar file. */
        Class c = null;
        try {
            c = Class.forName("edu.sc.seis.TauP.TauModelLoader");
            InputStream in = c.getResourceAsStream(packageName + "/" + filename);
            if (in != null) {
                return TauModel.readModelFromStream(in);
            }
        } catch (InvalidClassException ex) {
            throw new TauModelException("TauModel file not compatible with current version, recreate: " + c.getResource(packageName + "/" + filename), ex);
        } catch (Exception ex) {
            // couldn't get as a resource, so keep going
            if (verbose)
                System.out.println("couldn't load as resource: " + filename + "\n message: " + ex.getMessage());
        }
        /* couldn't find as a resource, try in classpath. */
        while (offset < classPath.length()) {
            pathSepIndex = classPath.indexOf(File.pathSeparatorChar, offset);
            if (pathSepIndex != -1) {
                pathEntry = classPath.substring(offset, pathSepIndex);
                offset = pathSepIndex + 1;
            } else {
                pathEntry = classPath.substring(offset);
                offset = classPath.length();
            }
            jarFile = new File(pathEntry);
            if (jarFile.exists() && jarFile.isFile() && jarFile.getName().equals("taup.jar") && jarFile.canRead()) {
                ZipFile zippy = new ZipFile(jarFile);
                ZipEntry zipEntry = zippy.getEntry("StdModels/" + filename);
                if (zipEntry != null) {
                    return TauModel.readModelFromStream(zippy.getInputStream(zipEntry));
                }
            }
        }
        /*
         * It isn't in the taup.jar so we try to find it within the paths
         * specified in the taup.model.path property.
         */
        offset = 0;
        if (taupPath != null) {
            while (offset < taupPath.length()) {
                pathSepIndex = taupPath.indexOf(File.pathSeparatorChar, offset);
                if (pathSepIndex != -1) {
                    pathEntry = taupPath.substring(offset, pathSepIndex);
                    offset = pathSepIndex + 1;
                } else {
                    pathEntry = taupPath.substring(offset);
                    offset = taupPath.length();
                }
                /* Check each jar file. */
                if (pathEntry.endsWith(".jar") || pathEntry.endsWith(".zip")) {
                    jarFile = new File(pathEntry);
                    if (jarFile.exists() && jarFile.isFile() && jarFile.canRead()) {
                        ZipFile zippy = new ZipFile(jarFile);
                        ZipEntry zipEntry = zippy.getEntry("Models/" + filename);
                        if (zipEntry != null) {
                            return TauModel.readModelFromStream(zippy.getInputStream(zipEntry));
                        }
                    }
                } else {
                    /* Check for regular files. */
                    modelFile = new File(pathEntry + "/" + filename);
                    if (modelFile.exists() && modelFile.isFile() && modelFile.canRead()) {
                        return TauModel.readModel(modelFile.getCanonicalPath());
                    }
                }
            }
        }
        /*
         * Couldn't find it in the taup.model.path either, look in the current
         * directory.
         */
        modelFile = new File(filename);
        if (modelFile.exists() && modelFile.isFile() && modelFile.canRead()) {
            return TauModel.readModel(modelFile.getCanonicalPath());
        }
        // try to load velocity model of same name and do a create
        try {
            VelocityModel vmod = loadVelocityModel(modelName);
            if (vmod != null) {
                TauP_Create tauPCreate = new TauP_Create();
                return tauPCreate.createTauModel(vmod);
            }
        } catch (Exception e) {
            throw new TauModelException("Can't find any saved models for " + modelName + " and creation from velocity model failed.", e);
        }
        throw new TauModelException("Can't find any saved models for " + modelName);
    } catch (InvalidClassException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    } catch (StreamCorruptedException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    } catch (OptionalDataException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    } catch (FileNotFoundException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    } catch (ClassNotFoundException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    } catch (IOException e) {
        throw new TauModelException("Unable to load '" + modelName + "'", e);
    }
}
Also used : InputStream(java.io.InputStream) InvalidClassException(java.io.InvalidClassException) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OptionalDataException(java.io.OptionalDataException) StreamCorruptedException(java.io.StreamCorruptedException) OptionalDataException(java.io.OptionalDataException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidClassException(java.io.InvalidClassException) ZipFile(java.util.zip.ZipFile) StreamCorruptedException(java.io.StreamCorruptedException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 40 with StreamCorruptedException

use of java.io.StreamCorruptedException in project connect-sdk-client-android by Ingenico-ePayments.

the class ReadInternalStorage method getIinResponsesFromCache.

/**
 * Reads all iinResponses from the cache on disk
 * @param context, used for reading files
 */
@SuppressWarnings("unchecked")
public Map<String, IinDetailsResponse> getIinResponsesFromCache() {
    if (context == null) {
        throw new InvalidParameterException("Error getting response in cache, context may not be null");
    }
    // Create new map which contains all the iinResponses
    Map<String, IinDetailsResponse> iinResponses = new HashMap<String, IinDetailsResponse>();
    // Check if the cachefile exists
    String directory = context.getFilesDir() + Constants.DIRECTORY_IINRESPONSES;
    File file = new File(directory, Constants.FILENAME_IINRESPONSE_CACHE);
    if (file.exists()) {
        // read the content and parse it to Map<String, IinDetailsResponse>
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            iinResponses = (Map<String, IinDetailsResponse>) objectInputStream.readObject();
        } catch (StreamCorruptedException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } catch (IOException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } finally {
            try {
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
            }
        }
    }
    return iinResponses;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) HashMap(java.util.HashMap) IinDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

StreamCorruptedException (java.io.StreamCorruptedException)53 IOException (java.io.IOException)35 ObjectInputStream (java.io.ObjectInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)22 OptionalDataException (java.io.OptionalDataException)11 File (java.io.File)8 FileInputStream (java.io.FileInputStream)7 FileNotFoundException (java.io.FileNotFoundException)7 InvalidClassException (java.io.InvalidClassException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ObjectOutputStream (java.io.ObjectOutputStream)6 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 EOFException (java.io.EOFException)4 NotSerializableException (java.io.NotSerializableException)4 Serializable (java.io.Serializable)4 Blob (java.sql.Blob)4 SSLException (javax.net.ssl.SSLException)4 FileOutputStream (java.io.FileOutputStream)3 SocketTimeoutException (java.net.SocketTimeoutException)3 InvalidParameterException (java.security.InvalidParameterException)3