Search in sources :

Example 41 with StreamCorruptedException

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

the class WriteInternalStorage method storeIinResponseInCache.

/**
 * Stores the given iinResponses in the cache on disk
 *
 * @param context, used for writing files
 * @param partialCreditCardNumber, entered partial creditcardnumber
 * @param IinDetailsResponse, the IinDetailsResponse which is added to the current cache
 */
public void storeIinResponseInCache(String partialCreditCardNumber, IinDetailsResponse iinResponse) {
    if (partialCreditCardNumber == null) {
        throw new InvalidParameterException("Error storing response in partialCreditCardNumber, context may not be null");
    }
    if (iinResponse == null) {
        throw new InvalidParameterException("Error storing response in iinResponse, iinResponse may not be null");
    }
    // Retrieve the currenct cached items and add the new one to it
    Map<String, IinDetailsResponse> currentCachedIinResponses = storage.getIinResponsesFromCache();
    // Overwrite old value if it exists
    if (currentCachedIinResponses.containsKey(partialCreditCardNumber)) {
        currentCachedIinResponses.remove(partialCreditCardNumber);
    }
    currentCachedIinResponses.put(partialCreditCardNumber, iinResponse);
    // Then write the currentCachedIinResponses to disk
    String directory = context.getFilesDir() + Constants.DIRECTORY_IINRESPONSES;
    File file = new File(directory, Constants.FILENAME_IINRESPONSE_CACHE);
    file.getParentFile().mkdirs();
    FileOutputStream fileOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(currentCachedIinResponses);
    } catch (StreamCorruptedException e) {
        Log.e(TAG, "Error storing BasicPaymentProducts on internal device storage", e);
    } catch (IOException e) {
        Log.e(TAG, "Error storing BasicPaymentProducts on internal device storage", e);
    } finally {
        try {
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IinDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse) FileOutputStream(java.io.FileOutputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Example 42 with StreamCorruptedException

use of java.io.StreamCorruptedException in project motech by motech.

the class MdsLongVarBinaryRDBMSMapping method deserialize.

private Object deserialize(byte[] bytes) {
    BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
    Bundle mdsEntitiesBundle = MdsBundleHelper.findMdsBundle(bundleContext);
    Bundle jodaTimeBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext, "joda-time");
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    try (ObjectInputStream ois = new MdsObjectInputStream(bais, mdsEntitiesBundle, jodaTimeBundle)) {
        return ois.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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Bundle(org.osgi.framework.Bundle) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) ObjectInputStream(java.io.ObjectInputStream)

Example 43 with StreamCorruptedException

use of java.io.StreamCorruptedException in project wifilibrary by LiShiHui24740.

the class SPUtils method readObject.

/**
 * 读取数据
 *
 * @param tag
 * @return
 */
public static Object readObject(SharedPreferences sharedPrefs, String tag) {
    Object object = null;
    String productBase64 = sharedPrefs.getString(tag, "");
    // 读取字节
    byte[] base64 = Base64.decode(productBase64, Base64.DEFAULT);
    // 封装到字节流
    ByteArrayInputStream bais = new ByteArrayInputStream(base64);
    try {
        // 再次封装
        ObjectInputStream bis = new ObjectInputStream(bais);
        try {
            // 读取对象
            object = bis.readObject();
        } catch (ClassNotFoundException e) {
            Log.d("AppConfig", e.toString());
        }
    } catch (StreamCorruptedException e) {
        Log.d("AppConfig", e.toString());
    } catch (IOException e) {
        Log.d("AppConfig", e.toString());
    }
    return object;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 44 with StreamCorruptedException

use of java.io.StreamCorruptedException in project teiid by teiid.

the class PgBackendProtocol method getContent.

private void getContent(ResultSet rs, PgColInfo col, int column) throws SQLException, TeiidSQLException, IOException {
    switch(col.type) {
        case PG_TYPE_BOOL:
            boolean b = rs.getBoolean(column);
            if (!rs.wasNull()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                writer.write(b ? "t" : "f");
            }
            break;
        case PG_TYPE_BPCHAR:
        case PG_TYPE_DATE:
        case PG_TYPE_FLOAT4:
        case PG_TYPE_FLOAT8:
        case PG_TYPE_INT2:
        case PG_TYPE_INT4:
        case PG_TYPE_INT8:
        case PG_TYPE_NUMERIC:
        case PG_TYPE_TIME:
        case PG_TYPE_TIMESTAMP_NO_TMZONE:
        case PG_TYPE_VARCHAR:
            String value = rs.getString(column);
            if (value != null) {
                writer.write(value);
            }
            break;
        case PG_TYPE_GEOMETRY:
            Object val = rs.getObject(column);
            if (val != null) {
                Blob blob = GeometryUtils.geometryToEwkb((GeometryType) rs.unwrap(ResultSetImpl.class).getRawCurrentValue());
                String hexewkb = PropertiesUtils.toHex(blob.getBytes(1, (int) blob.length()));
                writer.write(hexewkb);
            }
            break;
        case PG_TYPE_XML:
        case PG_TYPE_TEXT:
            Reader r = rs.getCharacterStream(column);
            if (r != null) {
                try {
                    ObjectConverterUtil.write(writer, r, this.maxLobSize, false);
                } finally {
                    r.close();
                }
            }
            break;
        case PG_TYPE_BYTEA:
            Blob blob = rs.getBlob(column);
            if (blob != null) {
                try {
                    String blobString = PGbytea.toPGString(ObjectConverterUtil.convertToByteArray(blob.getBinaryStream(), this.maxLobSize));
                    writer.write(blobString);
                } catch (OutOfMemoryError e) {
                    // $NON-NLS-1$
                    throw new StreamCorruptedException("data too big: " + e.getMessage());
                }
            }
            break;
        case PG_TYPE_CHARARRAY:
        case PG_TYPE_TEXTARRAY:
        case PG_TYPE_OIDARRAY:
        case PG_TYPE_BOOLARRAY:
        case PG_TYPE_INT2ARRAY:
        case PG_TYPE_INT4ARRAY:
        case PG_TYPE_INT8ARRAY:
        case PG_TYPE_FLOAT4ARRAY:
        case PG_TYPE_FLOAT8ARRAY:
        case PG_TYPE_NUMERICARRAY:
        case PG_TYPE_DATEARRAY:
        case PG_TYPE_TIMEARRAY:
        case PG_TYPE_TIMESTAMP_NO_TMZONEARRAY:
            {
                Array obj = rs.getArray(column);
                if (obj != null) {
                    writer.append("{");
                    boolean first = true;
                    Object array = obj.getArray();
                    int length = java.lang.reflect.Array.getLength(array);
                    for (int i = 0; i < length; i++) {
                        if (!first) {
                            writer.append(",");
                        } else {
                            first = false;
                        }
                        Object o = java.lang.reflect.Array.get(array, i);
                        if (o != null) {
                            if (col.type == PG_TYPE_TEXTARRAY) {
                                escapeQuote(writer, o.toString());
                            } else {
                                writer.append(o.toString());
                            }
                        }
                    }
                    writer.append("}");
                }
            }
            break;
        case PG_TYPE_INT2VECTOR:
        case PG_TYPE_OIDVECTOR:
            {
                ArrayImpl obj = (ArrayImpl) rs.getObject(column);
                if (obj != null) {
                    boolean first = true;
                    for (Object o : obj.getValues()) {
                        if (!first) {
                            writer.append(" ");
                        } else {
                            first = false;
                        }
                        if (o != null) {
                            writer.append(o.toString());
                        } else {
                            writer.append('0');
                        }
                    }
                }
            }
            break;
        default:
            Object obj = rs.getObject(column);
            if (obj != null) {
                throw new TeiidSQLException("unknown datatype " + col.type + " failed to convert");
            }
            break;
    }
}
Also used : Array(java.sql.Array) Blob(java.sql.Blob) ResultSetImpl(org.teiid.jdbc.ResultSetImpl) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ArrayImpl(org.teiid.core.types.ArrayImpl) Reader(java.io.Reader) StreamCorruptedException(java.io.StreamCorruptedException)

Example 45 with StreamCorruptedException

use of java.io.StreamCorruptedException in project teiid by teiid.

the class PgBackendProtocol method getBinaryContent.

private void getBinaryContent(ResultSet rs, PgColInfo col, int column) throws SQLException, TeiidSQLException, IOException {
    switch(col.type) {
        case PG_TYPE_INT2:
            short sval = rs.getShort(column);
            if (!rs.wasNull()) {
                dataOut.writeShort(sval);
            }
            break;
        case PG_TYPE_INT4:
            int ival = rs.getInt(column);
            if (!rs.wasNull()) {
                dataOut.writeInt(ival);
            }
            break;
        case PG_TYPE_INT8:
            long lval = rs.getLong(column);
            if (!rs.wasNull()) {
                dataOut.writeLong(lval);
            }
            break;
        case PG_TYPE_FLOAT4:
            float fval = rs.getFloat(column);
            if (!rs.wasNull()) {
                dataOut.writeInt(Float.floatToIntBits(fval));
            }
            break;
        case PG_TYPE_FLOAT8:
            double dval = rs.getDouble(column);
            if (!rs.wasNull()) {
                dataOut.writeLong(Double.doubleToLongBits(dval));
            }
            break;
        case PG_TYPE_BYTEA:
            Blob blob = rs.getBlob(column);
            if (blob != null) {
                try {
                    byte[] bytes = ObjectConverterUtil.convertToByteArray(blob.getBinaryStream(), this.maxLobSize);
                    write(bytes);
                } catch (OutOfMemoryError e) {
                    // $NON-NLS-1$
                    throw new StreamCorruptedException("data too big: " + e.getMessage());
                }
            }
            break;
        case PG_TYPE_DATE:
            Date d = rs.getDate(column);
            if (d != null) {
                long millis = d.getTime();
                millis += TimestampWithTimezone.getCalendar().getTimeZone().getOffset(millis);
                long secs = TimestampUtils.toPgSecs(millis / 1000);
                dataOut.writeInt((int) (secs / 86400));
            }
            break;
        default:
            throw new AssertionError();
    }
}
Also used : Blob(java.sql.Blob) StreamCorruptedException(java.io.StreamCorruptedException) Date(java.sql.Date)

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