Search in sources :

Example 21 with BufferOverflowException

use of java.nio.BufferOverflowException in project robovm by robovm.

the class FloatBufferTest method testPutfloat.

/*
     * Class under test for java.nio.FloatBuffer put(float)
     */
public void testPutfloat() {
    buf.clear();
    for (int i = 0; i < buf.capacity(); i++) {
        assertEquals(buf.position(), i);
        FloatBuffer ret = buf.put((float) i);
        assertEquals(buf.get(i), (float) i, 0.0);
        assertSame(ret, buf);
    }
    try {
        buf.put(0);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException e) {
    // expected
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) BufferOverflowException(java.nio.BufferOverflowException)

Example 22 with BufferOverflowException

use of java.nio.BufferOverflowException in project platform_frameworks_base by android.

the class AndroidKeyStoreCipherSpiBase method engineDoFinal.

@Override
protected final int engineDoFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    if (input == null) {
        throw new NullPointerException("input == null");
    }
    if (output == null) {
        throw new NullPointerException("output == null");
    }
    int inputSize = input.remaining();
    byte[] outputArray;
    if (input.hasArray()) {
        outputArray = engineDoFinal(input.array(), input.arrayOffset() + input.position(), inputSize);
        input.position(input.position() + inputSize);
    } else {
        byte[] inputArray = new byte[inputSize];
        input.get(inputArray);
        outputArray = engineDoFinal(inputArray, 0, inputSize);
    }
    int outputSize = (outputArray != null) ? outputArray.length : 0;
    if (outputSize > 0) {
        int outputBufferAvailable = output.remaining();
        try {
            output.put(outputArray);
        } catch (BufferOverflowException e) {
            throw new ShortBufferException("Output buffer too small. Produced: " + outputSize + ", available: " + outputBufferAvailable);
        }
    }
    return outputSize;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) BufferOverflowException(java.nio.BufferOverflowException)

Example 23 with BufferOverflowException

use of java.nio.BufferOverflowException in project platform_frameworks_base by android.

the class RouterAdvertisementDaemon method assembleRaLocked.

private void assembleRaLocked() {
    final ByteBuffer ra = ByteBuffer.wrap(mRA);
    ra.order(ByteOrder.BIG_ENDIAN);
    boolean shouldSendRA = false;
    try {
        putHeader(ra, mRaParams != null && mRaParams.hasDefaultRoute);
        putSlla(ra, mHwAddr);
        mRaLength = ra.position();
        if (mRaParams != null) {
            putMtu(ra, mRaParams.mtu);
            mRaLength = ra.position();
            for (IpPrefix ipp : mRaParams.prefixes) {
                putPio(ra, ipp, DEFAULT_LIFETIME, DEFAULT_LIFETIME);
                mRaLength = ra.position();
                shouldSendRA = true;
            }
            if (mRaParams.dnses.size() > 0) {
                putRdnss(ra, mRaParams.dnses, DEFAULT_LIFETIME);
                mRaLength = ra.position();
                shouldSendRA = true;
            }
        }
        for (IpPrefix ipp : mDeprecatedInfoTracker.getPrefixes()) {
            putPio(ra, ipp, 0, 0);
            mRaLength = ra.position();
            shouldSendRA = true;
        }
        final Set<Inet6Address> deprecatedDnses = mDeprecatedInfoTracker.getDnses();
        if (!deprecatedDnses.isEmpty()) {
            putRdnss(ra, deprecatedDnses, 0);
            mRaLength = ra.position();
            shouldSendRA = true;
        }
    } catch (BufferOverflowException e) {
        // The packet up to mRaLength  is valid, since it has been updated
        // progressively as the RA was built. Log an error, and continue
        // on as best as possible.
        Log.e(TAG, "Could not construct new RA: " + e);
    }
    // We have nothing worth announcing; indicate as much to maybeSendRA().
    if (!shouldSendRA) {
        mRaLength = 0;
    }
}
Also used : IpPrefix(android.net.IpPrefix) Inet6Address(java.net.Inet6Address) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer)

Example 24 with BufferOverflowException

use of java.nio.BufferOverflowException in project flink by apache.

the class MemorySegmentTestBase method testByteBufferOutOfBounds.

// ------------------------------------------------------------------------
//  ByteBuffer overflow / underflow and out of bounds
// ------------------------------------------------------------------------
@Test
public void testByteBufferOutOfBounds() {
    try {
        final int bbCapacity = pageSize / 10;
        final int[] validOffsets = { 0, 1, pageSize / 10 * 9 };
        final int[] invalidOffsets = { -1, pageSize + 1, -pageSize, Integer.MAX_VALUE, Integer.MIN_VALUE };
        final int[] validLengths = { 0, 1, bbCapacity, pageSize };
        final int[] invalidLengths = { -1, -pageSize, Integer.MAX_VALUE, Integer.MIN_VALUE };
        final MemorySegment seg = createSegment(pageSize);
        for (ByteBuffer bb : new ByteBuffer[] { ByteBuffer.allocate(bbCapacity), ByteBuffer.allocateDirect(bbCapacity) }) {
            for (int off : validOffsets) {
                for (int len : invalidLengths) {
                    try {
                        seg.put(off, bb, len);
                        fail("should fail with an IndexOutOfBoundsException");
                    } catch (IndexOutOfBoundsException | BufferUnderflowException ignored) {
                    }
                    try {
                        seg.get(off, bb, len);
                        fail("should fail with an IndexOutOfBoundsException");
                    } catch (IndexOutOfBoundsException | BufferOverflowException ignored) {
                    }
                    // position/limit may not have changed
                    assertEquals(0, bb.position());
                    assertEquals(bb.capacity(), bb.limit());
                }
            }
            for (int off : invalidOffsets) {
                for (int len : validLengths) {
                    try {
                        seg.put(off, bb, len);
                        fail("should fail with an IndexOutOfBoundsException");
                    } catch (IndexOutOfBoundsException | BufferUnderflowException ignored) {
                    }
                    try {
                        seg.get(off, bb, len);
                        fail("should fail with an IndexOutOfBoundsException");
                    } catch (IndexOutOfBoundsException | BufferOverflowException ignored) {
                    }
                    // position/limit may not have changed
                    assertEquals(0, bb.position());
                    assertEquals(bb.capacity(), bb.limit());
                }
            }
            for (int off : validOffsets) {
                for (int len : validLengths) {
                    if (off + len > pageSize) {
                        try {
                            seg.put(off, bb, len);
                            fail("should fail with an IndexOutOfBoundsException");
                        } catch (IndexOutOfBoundsException | BufferUnderflowException ignored) {
                        }
                        try {
                            seg.get(off, bb, len);
                            fail("should fail with an IndexOutOfBoundsException");
                        } catch (IndexOutOfBoundsException | BufferOverflowException ignored) {
                        }
                        // position/limit may not have changed
                        assertEquals(0, bb.position());
                        assertEquals(bb.capacity(), bb.limit());
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 25 with BufferOverflowException

use of java.nio.BufferOverflowException in project bazel by bazelbuild.

the class DexFileAggregator method merge.

private Dex merge(Dex... dexes) throws IOException {
    switch(dexes.length) {
        case 0:
            return new Dex(0);
        case 1:
            return dexes[0];
        default:
            try {
                DexMerger dexMerger = new DexMerger(dexes, CollisionPolicy.FAIL);
                dexMerger.setCompactWasteThreshold(wasteThresholdPerDex);
                return dexMerger.merge();
            } catch (BufferOverflowException e) {
                // Bug in dx can cause this for ~1500 or more classes
                Dex[] left = Arrays.copyOf(dexes, dexes.length / 2);
                Dex[] right = Arrays.copyOfRange(dexes, left.length, dexes.length);
                System.err.printf("Couldn't merge %d classes, trying %d%n", dexes.length, left.length);
                try {
                    return merge(merge(left), merge(right));
                } catch (RuntimeException e2) {
                    e2.addSuppressed(e);
                    throw e2;
                }
            }
    }
}
Also used : Dex(com.android.dex.Dex) DexMerger(com.android.dx.merge.DexMerger) BufferOverflowException(java.nio.BufferOverflowException)

Aggregations

BufferOverflowException (java.nio.BufferOverflowException)77 ByteBuffer (java.nio.ByteBuffer)27 ShortBufferException (javax.crypto.ShortBufferException)10 CharBuffer (java.nio.CharBuffer)9 ShortBuffer (java.nio.ShortBuffer)7 Test (org.junit.Test)7 FloatBuffer (java.nio.FloatBuffer)6 IntBuffer (java.nio.IntBuffer)6 BufferUnderflowException (java.nio.BufferUnderflowException)5 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)5 IpPrefix (android.net.IpPrefix)4 IOException (java.io.IOException)4 Inet6Address (java.net.Inet6Address)4 DoubleBuffer (java.nio.DoubleBuffer)4 LongBuffer (java.nio.LongBuffer)4 MappedByteBuffer (java.nio.MappedByteBuffer)4 Pointer (com.cetsoft.imcache.offheap.bytebuffer.Pointer)2 EOFException (java.io.EOFException)2 CoderResult (java.nio.charset.CoderResult)2 JSONObject (org.json_voltpatches.JSONObject)2