Search in sources :

Example 11 with StreamCorruptedException

use of java.io.StreamCorruptedException in project BookReader by JustWayward.

the class SharedPreferencesUtil method getObject.

public <T> T getObject(String key, Class<T> clazz) {
    if (prefs.contains(key)) {
        String objectVal = prefs.getString(key, null);
        byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(bais);
            T t = (T) ois.readObject();
            return t;
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with StreamCorruptedException

use of java.io.StreamCorruptedException in project jPOS by jpos.

the class BSHGroupSelectorTest method testDefaultSelect.

@Test
public void testDefaultSelect() throws Throwable {
    String result = new BSHGroupSelector().defaultSelect(100L, new StreamCorruptedException());
    assertEquals("result", "", result);
}
Also used : StreamCorruptedException(java.io.StreamCorruptedException) Test(org.junit.Test)

Example 13 with StreamCorruptedException

use of java.io.StreamCorruptedException in project elasticsearch by elastic.

the class TcpTransport method validateMessageHeader.

/**
     * Validates the first N bytes of the message header and returns <code>false</code> if the message is
     * a ping message and has no payload ie. isn't a real user level message.
     *
     * @throws IllegalStateException if the message is too short, less than the header or less that the header plus the message size
     * @throws HttpOnTransportException if the message has no valid header and appears to be a HTTP message
     * @throws IllegalArgumentException if the message is greater that the maximum allowed frame size. This is dependent on the available
     * memory.
     */
public static boolean validateMessageHeader(BytesReference buffer) throws IOException {
    final int sizeHeaderLength = TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE;
    if (buffer.length() < sizeHeaderLength) {
        throw new IllegalStateException("message size must be >= to the header size");
    }
    int offset = 0;
    if (buffer.get(offset) != 'E' || buffer.get(offset + 1) != 'S') {
        // special handling for what is probably HTTP
        if (bufferStartsWith(buffer, offset, "GET ") || bufferStartsWith(buffer, offset, "POST ") || bufferStartsWith(buffer, offset, "PUT ") || bufferStartsWith(buffer, offset, "HEAD ") || bufferStartsWith(buffer, offset, "DELETE ") || bufferStartsWith(buffer, offset, "OPTIONS ") || bufferStartsWith(buffer, offset, "PATCH ") || bufferStartsWith(buffer, offset, "TRACE ")) {
            throw new HttpOnTransportException("This is not a HTTP port");
        }
        // we have 6 readable bytes, show 4 (should be enough)
        throw new StreamCorruptedException("invalid internal transport message format, got (" + Integer.toHexString(buffer.get(offset) & 0xFF) + "," + Integer.toHexString(buffer.get(offset + 1) & 0xFF) + "," + Integer.toHexString(buffer.get(offset + 2) & 0xFF) + "," + Integer.toHexString(buffer.get(offset + 3) & 0xFF) + ")");
    }
    final int dataLen;
    try (StreamInput input = buffer.streamInput()) {
        input.skip(TcpHeader.MARKER_BYTES_SIZE);
        dataLen = input.readInt();
        if (dataLen == PING_DATA_SIZE) {
            // and returning null
            return false;
        }
    }
    if (dataLen <= 0) {
        throw new StreamCorruptedException("invalid data length: " + dataLen);
    }
    // safety against too large frames being sent
    if (dataLen > NINETY_PER_HEAP_SIZE) {
        throw new IllegalArgumentException("transport content length received [" + new ByteSizeValue(dataLen) + "] exceeded [" + new ByteSizeValue(NINETY_PER_HEAP_SIZE) + "]");
    }
    if (buffer.length() < dataLen + sizeHeaderLength) {
        throw new IllegalStateException("buffer must be >= to the message size but wasn't");
    }
    return true;
}
Also used : NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) StreamCorruptedException(java.io.StreamCorruptedException)

Example 14 with StreamCorruptedException

use of java.io.StreamCorruptedException in project carat by amplab.

the class CaratSampleDB method fillSample.

/*
     * Read a sample from the current position of the cursor. TODO: Needs to be
     * updated when fields update.
     */
private Sample fillSample(Cursor cursor) {
    Sample s = null;
    byte[] sampleB = cursor.getBlob(cursor.getColumnIndex(CaratSampleDB.COLUMN_SAMPLE));
    if (sampleB != null) {
        ObjectInputStream oi;
        try {
            oi = new ObjectInputStream(new ByteArrayInputStream(sampleB));
            Object o = oi.readObject();
            if (o != null)
                s = SampleReader.readSample(o);
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    return s;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with StreamCorruptedException

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

the class Test method test_readObjectCorrupt.

public void test_readObjectCorrupt() {
    byte[] bytes = { 00, 00, 00, 0x64, 0x43, 0x48, (byte) 0xFD, 0x71, 00, 00, 0x0B, (byte) 0xB8, 0x4D, 0x65 };
    ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
    boolean exception = false;
    try {
        ObjectInputStream in = new ObjectInputStream(bin);
        in.readObject();
        fail("Unexpected read of corrupted stream");
    } catch (StreamCorruptedException e) {
        exception = true;
    } catch (IOException e) {
        fail("Unexpected: " + e);
    } catch (ClassNotFoundException e) {
        fail("Unexpected: " + e);
    }
    assertTrue("Expected StreamCorruptedException", exception);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

StreamCorruptedException (java.io.StreamCorruptedException)26 IOException (java.io.IOException)18 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)11 OptionalDataException (java.io.OptionalDataException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InvalidClassException (java.io.InvalidClassException)5 ObjectOutputStream (java.io.ObjectOutputStream)5 NotSerializableException (java.io.NotSerializableException)4 Serializable (java.io.Serializable)4 SSLException (javax.net.ssl.SSLException)4 EOFException (java.io.EOFException)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 Socket (java.net.Socket)2 UUID (java.util.UUID)2