use of java.nio.ReadOnlyBufferException in project robovm by robovm.
the class SSLEngineTest method test_unwrap_ByteBuffer$ByteBuffer_02.
/**
* javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
* ReadOnlyBufferException should be thrown.
*/
@KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
public void test_unwrap_ByteBuffer$ByteBuffer_02() {
String host = "new host";
int port = 8080;
ByteBuffer bbs = ByteBuffer.allocate(10);
ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
SSLEngine sse = getEngine(host, port);
sse.setUseClientMode(true);
try {
sse.unwrap(bbs, bbA);
fail("ReadOnlyBufferException wasn't thrown");
} catch (ReadOnlyBufferException iobe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of ReadOnlyBufferException");
}
}
use of java.nio.ReadOnlyBufferException in project tomcat by apache.
the class OpenSSLEngine method wrap.
@Override
public synchronized SSLEngineResult wrap(final ByteBuffer[] srcs, final int offset, final int length, final ByteBuffer dst) throws SSLException {
// Check to make sure the engine has not been closed
if (destroyed) {
return new SSLEngineResult(SSLEngineResult.Status.CLOSED, SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
// Throw required runtime exceptions
if (srcs == null || dst == null) {
throw new IllegalArgumentException(sm.getString("engine.nullBuffer"));
}
if (offset >= srcs.length || offset + length > srcs.length) {
throw new IndexOutOfBoundsException(sm.getString("engine.invalidBufferArray", Integer.toString(offset), Integer.toString(length), Integer.toString(srcs.length)));
}
if (dst.isReadOnly()) {
throw new ReadOnlyBufferException();
}
// Prepare OpenSSL to work in server mode and receive handshake
if (accepted == 0) {
beginHandshakeImplicitly();
}
// In handshake or close_notify stages, check if call to wrap was made
// without regard to the handshake status.
SSLEngineResult.HandshakeStatus handshakeStatus = getHandshakeStatus();
if ((!handshakeFinished || engineClosed) && handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
return new SSLEngineResult(getEngineStatus(), SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);
}
int bytesProduced = 0;
int pendingNet;
// Check for pending data in the network BIO
pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
if (pendingNet > 0) {
// Do we have enough room in destination to write encrypted data?
int capacity = dst.remaining();
if (capacity < pendingNet) {
return new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, handshakeStatus, 0, 0);
}
// Write the pending data from the network BIO into the dst buffer
try {
bytesProduced = readEncryptedData(dst, pendingNet);
} catch (Exception e) {
throw new SSLException(e);
}
// for the receipt the peer's close_notify message -- shutdown.
if (isOutboundDone) {
shutdown();
}
return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), 0, bytesProduced);
}
// There was no pending data in the network BIO -- encrypt any application data
int bytesConsumed = 0;
int endOffset = offset + length;
for (int i = offset; i < endOffset; ++i) {
final ByteBuffer src = srcs[i];
if (src == null) {
throw new IllegalArgumentException(sm.getString("engine.nullBufferInArray"));
}
while (src.hasRemaining()) {
// Write plain text application data to the SSL engine
try {
bytesConsumed += writePlaintextData(src);
} catch (Exception e) {
throw new SSLException(e);
}
// Check to see if the engine wrote data into the network BIO
pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
if (pendingNet > 0) {
// Do we have enough room in dst to write encrypted data?
int capacity = dst.remaining();
if (capacity < pendingNet) {
return new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, getHandshakeStatus(), bytesConsumed, bytesProduced);
}
// Write the pending data from the network BIO into the dst buffer
try {
bytesProduced += readEncryptedData(dst, pendingNet);
} catch (Exception e) {
throw new SSLException(e);
}
return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
}
}
}
return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
}
use of java.nio.ReadOnlyBufferException in project buck by facebook.
the class ObjectFileScrubbers method createDateUidGidScrubber.
public static FileContentsScrubber createDateUidGidScrubber(final PaddingStyle paddingStyle) {
return new FileContentsScrubber() {
/**
* Efficiently modifies the archive backed by the given buffer to remove any non-deterministic
* meta-data such as timestamps, UIDs, and GIDs.
*/
@SuppressWarnings("PMD.AvoidUsingOctalValues")
@Override
public void scrubFile(FileChannel file) throws IOException, ScrubException {
try {
ByteBuffer header = ByteBuffer.allocate(GLOBAL_HEADER_SIZE);
file.read(header);
// Grab the global header chunk and verify it's accurate.
header.position(0);
byte[] globalHeader = getBytes(header, GLOBAL_HEADER_SIZE);
boolean thin = checkHeader(globalHeader);
// Iterate over all the file meta-data entries, injecting zero's for timestamp,
// UID, and GID.
final int entrySize = 16 + /* fileName */
12 + /* file modification time */
6 + /* owner ID */
6 + /* group ID */
8 + /* file mode */
10 + /* file size */
2;
long start = GLOBAL_HEADER_SIZE;
ByteBuffer buffer = ByteBuffer.allocate(entrySize);
while (start < file.size()) {
checkArchive(file.size() - start >= entrySize, "Invalid entry metadata format");
buffer.clear();
file.position(start);
int read = file.read(buffer);
checkArchive(read == entrySize, "Not all bytes have been read");
// position points just past the last byte read, so need to reset
buffer.position(0);
String fileName = new String(getBytes(buffer, 16), Charsets.US_ASCII).trim();
// Inject 0's for the non-deterministic meta-data entries.
/* File modification timestamp */
putIntAsDecimalString(buffer, 12, ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, paddingStyle);
/* Owner ID */
putIntAsDecimalString(buffer, 6, 0, paddingStyle);
/* Group ID */
putIntAsDecimalString(buffer, 6, 0, paddingStyle);
/* File mode */
putIntAsOctalString(buffer, 8, 0100644, paddingStyle);
long fileSize = getDecimalStringAsLong(buffer, 10);
// Lastly, grab the file magic entry and verify it's accurate.
byte[] fileMagic = getBytes(buffer, 2);
checkArchive(Arrays.equals(END_OF_FILE_HEADER_MARKER, fileMagic), "invalid file magic");
// write the changes
// position points just past the last byte accessed, need to reset
buffer.position(0);
file.position(start);
int written = file.write(buffer);
checkArchive(written == entrySize, "Not all bytes have been written");
// Skip the file data.
start += entrySize;
if (!thin || SPECIAL_ENTRIES.contains(fileName)) {
start += fileSize + fileSize % 2;
}
}
// Convert any low-level exceptions to `ArchiveExceptions`s.
} catch (BufferUnderflowException | ReadOnlyBufferException e) {
throw new ScrubException(e.getMessage());
}
}
};
}
use of java.nio.ReadOnlyBufferException in project j2objc by google.
the class BufferTest method testPutByteBuffer.
// http://code.google.com/p/android/issues/detail?id=16184
public void testPutByteBuffer() throws Exception {
ByteBuffer dst = ByteBuffer.allocate(10).asReadOnlyBuffer();
// Can't put into a read-only buffer.
try {
dst.put(ByteBuffer.allocate(5));
fail();
} catch (ReadOnlyBufferException expected) {
}
// Can't put a buffer into itself.
dst = ByteBuffer.allocate(10);
try {
dst.put(dst);
fail();
} catch (IllegalArgumentException expected) {
}
// Can't put the null ByteBuffer.
try {
dst.put((ByteBuffer) null);
fail();
} catch (NullPointerException expected) {
}
// Can't put a larger source into a smaller destination.
try {
dst.put(ByteBuffer.allocate(dst.capacity() + 1));
fail();
} catch (BufferOverflowException expected) {
}
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), true);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), true);
assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), true);
}
use of java.nio.ReadOnlyBufferException in project netty by netty.
the class AbstractByteBufTest method testGetReadOnlyDst.
private void testGetReadOnlyDst(boolean direct) {
byte[] bytes = { 'a', 'b', 'c', 'd' };
ByteBuf buffer = newBuffer(bytes.length);
buffer.writeBytes(bytes);
ByteBuffer dst = direct ? ByteBuffer.allocateDirect(bytes.length) : ByteBuffer.allocate(bytes.length);
ByteBuffer readOnlyDst = dst.asReadOnlyBuffer();
try {
buffer.getBytes(0, readOnlyDst);
fail();
} catch (ReadOnlyBufferException e) {
// expected
}
assertEquals(0, readOnlyDst.position());
buffer.release();
}
Aggregations