Search in sources :

Example 51 with BufferOverflowException

use of java.nio.BufferOverflowException in project j2objc by google.

the class BufferTest method assertPutByteBuffer.

private void assertPutByteBuffer(ByteBuffer dst, ByteBuffer src, boolean readOnly) {
    // Start 'dst' off as the index-identity pattern.
    for (int i = 0; i < dst.capacity(); ++i) {
        dst.put(i, (byte) i);
    }
    // Deliberately offset the position we'll write to by 1.
    dst.position(1);
    // Make the source more interesting.
    for (int i = 0; i < src.capacity(); ++i) {
        src.put(i, (byte) (16 + i));
    }
    if (readOnly) {
        src = src.asReadOnlyBuffer();
    }
    ByteBuffer dst2 = dst.put(src);
    assertSame(dst, dst2);
    assertEquals(0, src.remaining());
    assertEquals(src.position(), src.capacity());
    assertEquals(dst.position(), src.capacity() + 1);
    for (int i = 0; i < src.capacity(); ++i) {
        assertEquals(src.get(i), dst.get(i + 1));
    }
    // No room for another.
    src.position(0);
    try {
        dst.put(src);
        fail();
    } catch (BufferOverflowException expected) {
    }
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 52 with BufferOverflowException

use of java.nio.BufferOverflowException in project DirectMemory by raffaeleguidi.

the class OffHeapMemoryBuffer method store.

private synchronized Pointer store(byte[] payload, long expiresIn, long expires) {
    Pointer goodOne = firstMatch(payload.length);
    if (goodOne == null) {
        throw new NullPointerException("did not find a suitable buffer");
    }
    Pointer fresh = slice(goodOne, payload.length);
    fresh.created = System.currentTimeMillis();
    if (expiresIn > 0) {
        fresh.expiresIn = expiresIn;
        fresh.expires = 0;
    } else if (expires > 0) {
        fresh.expiresIn = 0;
        fresh.expires = expires;
    }
    fresh.free = false;
    used.addAndGet(payload.length);
    ByteBuffer buf = buffer.slice();
    buf.position(fresh.start);
    try {
        buf.put(payload);
    } catch (BufferOverflowException e) {
        // RpG not convincing - let's fix it later
        goodOne.start = fresh.start;
        goodOne.end = buffer.limit();
        return null;
    }
    pointers.add(fresh);
    return fresh;
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer)

Example 53 with BufferOverflowException

use of java.nio.BufferOverflowException in project undertow by undertow-io.

the class AjpClientRequestClientStreamSinkChannel method createFrameHeaderImpl.

private SendFrameHeader createFrameHeaderImpl() {
    if (discardMode) {
        getBuffer().clear();
        getBuffer().flip();
        return new SendFrameHeader(new ImmediatePooledByteBuffer(ByteBuffer.wrap(new byte[0])));
    }
    PooledByteBuffer pooledHeaderBuffer = getChannel().getBufferPool().allocate();
    try {
        final ByteBuffer buffer = pooledHeaderBuffer.getBuffer();
        ByteBuffer dataBuffer = getBuffer();
        int dataInBuffer = dataBuffer.remaining();
        if (!firstFrameWritten && requestedChunkSize == 0) {
            //we are waiting on a read body chunk
            return new SendFrameHeader(dataInBuffer, null);
        }
        int maxData = getChannel().getSettings().get(UndertowOptions.MAX_AJP_PACKET_SIZE, DEFAULT_MAX_DATA_SIZE) - 6;
        if (!firstFrameWritten) {
            String contentLength = headers.getFirst(Headers.CONTENT_LENGTH);
            if (contentLength != null) {
                dataSize = Long.parseLong(contentLength);
                requestedChunkSize = maxData;
                if (dataInBuffer > dataSize) {
                    throw UndertowMessages.MESSAGES.fixedLengthOverflow();
                }
            } else if (isWritesShutdown() && !headers.contains(Headers.TRANSFER_ENCODING)) {
                //writes are shut down, go to fixed length
                headers.put(Headers.CONTENT_LENGTH, dataInBuffer);
                dataSize = dataInBuffer;
                requestedChunkSize = maxData;
            } else {
                headers.put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString());
                dataSize = -1;
                requestedChunkSize = 0;
            }
            firstFrameWritten = true;
            final String path;
            final String queryString;
            int qsIndex = this.path.indexOf('?');
            if (qsIndex == -1) {
                path = this.path;
                queryString = null;
            } else {
                path = this.path.substring(0, qsIndex);
                queryString = this.path.substring(qsIndex + 1);
            }
            buffer.put((byte) 0x12);
            buffer.put((byte) 0x34);
            //we fill the size in later
            buffer.put((byte) 0);
            buffer.put((byte) 0);
            buffer.put((byte) 2);
            boolean storeMethod = false;
            Integer methodNp = AjpConstants.HTTP_METHODS_MAP.get(method);
            if (methodNp == null) {
                methodNp = 0xFF;
                storeMethod = true;
            }
            buffer.put((byte) (int) methodNp);
            AjpUtils.putHttpString(buffer, protocol);
            putString(buffer, path);
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.REMOTE_ADDRESS)));
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.REMOTE_HOST)));
            putString(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.SERVER_NAME)));
            AjpUtils.putInt(buffer, notNull(attachable.getAttachment(ProxiedRequestAttachments.SERVER_PORT)));
            buffer.put((byte) (notNull(attachable.getAttachment(ProxiedRequestAttachments.IS_SSL)) ? 1 : 0));
            int headers = 0;
            //we need to count the headers
            final HeaderMap responseHeaders = this.headers;
            for (HttpString name : responseHeaders.getHeaderNames()) {
                headers += responseHeaders.get(name).size();
            }
            AjpUtils.putInt(buffer, headers);
            for (final HttpString header : responseHeaders.getHeaderNames()) {
                for (String headerValue : responseHeaders.get(header)) {
                    Integer headerCode = AjpConstants.HEADER_MAP.get(header);
                    if (headerCode != null) {
                        AjpUtils.putInt(buffer, headerCode);
                    } else {
                        AjpUtils.putHttpString(buffer, header);
                    }
                    putString(buffer, headerValue);
                }
            }
            if (queryString != null) {
                //query_string
                buffer.put((byte) ATTR_QUERY_STRING);
                putString(buffer, queryString);
            }
            String remoteUser = attachable.getAttachment(ProxiedRequestAttachments.REMOTE_USER);
            if (remoteUser != null) {
                buffer.put((byte) ATTR_REMOTE_USER);
                putString(buffer, remoteUser);
            }
            String authType = attachable.getAttachment(ProxiedRequestAttachments.AUTH_TYPE);
            if (authType != null) {
                buffer.put((byte) ATTR_AUTH_TYPE);
                putString(buffer, authType);
            }
            String route = attachable.getAttachment(ProxiedRequestAttachments.ROUTE);
            if (route != null) {
                buffer.put((byte) ATTR_ROUTE);
                putString(buffer, route);
            }
            String sslCert = attachable.getAttachment(ProxiedRequestAttachments.SSL_CERT);
            if (sslCert != null) {
                buffer.put((byte) ATTR_SSL_CERT);
                putString(buffer, sslCert);
            }
            String sslCypher = attachable.getAttachment(ProxiedRequestAttachments.SSL_CYPHER);
            if (sslCypher != null) {
                buffer.put((byte) ATTR_SSL_CIPHER);
                putString(buffer, sslCypher);
            }
            byte[] sslSession = attachable.getAttachment(ProxiedRequestAttachments.SSL_SESSION_ID);
            if (sslSession != null) {
                buffer.put((byte) ATTR_SSL_SESSION);
                putString(buffer, FlexBase64.encodeString(sslSession, false));
            }
            Integer sslKeySize = attachable.getAttachment(ProxiedRequestAttachments.SSL_KEY_SIZE);
            if (sslKeySize != null) {
                buffer.put((byte) ATTR_SSL_KEY_SIZE);
                putString(buffer, sslKeySize.toString());
            }
            String secret = attachable.getAttachment(ProxiedRequestAttachments.SECRET);
            if (secret != null) {
                buffer.put((byte) ATTR_SECRET);
                putString(buffer, secret);
            }
            if (storeMethod) {
                buffer.put((byte) ATTR_STORED_METHOD);
                putString(buffer, method.toString());
            }
            buffer.put((byte) 0xFF);
            int dataLength = buffer.position() - 4;
            buffer.put(2, (byte) ((dataLength >> 8) & 0xFF));
            buffer.put(3, (byte) (dataLength & 0xFF));
        }
        if (dataSize == 0) {
            //no data, just write out this frame and we are done
            buffer.flip();
            return new SendFrameHeader(pooledHeaderBuffer);
        } else if (requestedChunkSize > 0) {
            if (isWritesShutdown() && dataInBuffer == 0) {
                buffer.put((byte) 0x12);
                buffer.put((byte) 0x34);
                buffer.put((byte) 0x00);
                buffer.put((byte) 0x02);
                buffer.put((byte) 0x00);
                buffer.put((byte) 0x00);
                buffer.flip();
                return new SendFrameHeader(pooledHeaderBuffer);
            }
            int remaining = dataInBuffer;
            remaining = Math.min(remaining, maxData);
            remaining = Math.min(remaining, requestedChunkSize);
            int bodySize = remaining + 2;
            buffer.put((byte) 0x12);
            buffer.put((byte) 0x34);
            buffer.put((byte) ((bodySize >> 8) & 0xFF));
            buffer.put((byte) (bodySize & 0xFF));
            buffer.put((byte) ((remaining >> 8) & 0xFF));
            buffer.put((byte) (remaining & 0xFF));
            requestedChunkSize = 0;
            if (remaining < dataInBuffer) {
                dataBuffer.limit(getBuffer().position() + remaining);
                buffer.flip();
                return new SendFrameHeader(dataInBuffer - remaining, pooledHeaderBuffer, dataSize < 0);
            } else {
                buffer.flip();
                return new SendFrameHeader(0, pooledHeaderBuffer, dataSize < 0);
            }
        } else {
            //chunked. We just write the headers, and leave all the data in the buffer
            //they need to send us a read body chunk in order to get any data
            buffer.flip();
            if (buffer.remaining() == 0) {
                pooledHeaderBuffer.close();
                return new SendFrameHeader(dataInBuffer, null, true);
            }
            dataBuffer.limit(dataBuffer.position());
            return new SendFrameHeader(dataInBuffer, pooledHeaderBuffer, true);
        }
    } catch (BufferOverflowException e) {
        //TODO: UNDERTOW-901
        pooledHeaderBuffer.close();
        markBroken();
        throw e;
    }
}
Also used : HeaderMap(io.undertow.util.HeaderMap) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) AjpUtils.putString(io.undertow.protocols.ajp.AjpUtils.putString) HttpString(io.undertow.util.HttpString) BufferOverflowException(java.nio.BufferOverflowException) SendFrameHeader(io.undertow.server.protocol.framed.SendFrameHeader) ByteBuffer(java.nio.ByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) HttpString(io.undertow.util.HttpString)

Example 54 with BufferOverflowException

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

the class AndroidKeyStoreCipherSpiBase method engineUpdate.

@Override
protected final int engineUpdate(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    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 = engineUpdate(input.array(), input.arrayOffset() + input.position(), inputSize);
        input.position(input.position() + inputSize);
    } else {
        byte[] inputArray = new byte[inputSize];
        input.get(inputArray);
        outputArray = engineUpdate(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 55 with BufferOverflowException

use of java.nio.BufferOverflowException in project android_frameworks_base by DirtyUnicorns.

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)

Aggregations

BufferOverflowException (java.nio.BufferOverflowException)78 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 IOException (java.io.IOException)5 BufferUnderflowException (java.nio.BufferUnderflowException)5 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)5 IpPrefix (android.net.IpPrefix)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