本文整理汇总了Java中javax.net.ssl.SSLEngineResult.bytesProduced方法的典型用法代码示例。如果您正苦于以下问题:Java SSLEngineResult.bytesProduced方法的具体用法?Java SSLEngineResult.bytesProduced怎么用?Java SSLEngineResult.bytesProduced使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.SSLEngineResult
的用法示例。
在下文中一共展示了SSLEngineResult.bytesProduced方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkResult
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
private void checkResult(SSLEngineResult result, boolean wrap)
throws SSLException {
handshakeStatus = result.getHandshakeStatus();
resultStatus = result.getStatus();
if (resultStatus != Status.OK &&
(wrap || resultStatus != Status.BUFFER_UNDERFLOW)) {
throw new SSLException("TODO");
}
if (wrap && result.bytesConsumed() != 0) {
throw new SSLException("TODO");
}
if (!wrap && result.bytesProduced() != 0) {
throw new SSLException("TODO");
}
}
示例2: checkResult
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
private void checkResult(SSLEngineResult result, boolean wrap) throws SSLException {
handshakeStatus = result.getHandshakeStatus();
resultStatus = result.getStatus();
if (resultStatus != Status.OK && (wrap || resultStatus != Status.BUFFER_UNDERFLOW)) {
throw new SSLException("TODO");
}
if (wrap && result.bytesConsumed() != 0) {
throw new SSLException("TODO");
}
if (!wrap && result.bytesProduced() != 0) {
throw new SSLException("TODO");
}
}
示例3: readMessage
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
private void readMessage() throws IOException {
int totalProduced = 0;
while (!stopping) {
SSLEngineResult result = unwrap();
if (result.getStatus() != Status.OK) {
throw new RuntimeException("Failed reading message: " + result);
}
totalProduced += result.bytesProduced();
if (totalProduced == MESSAGE_LENGTH) {
return;
}
}
}
示例4: writeInternal
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
private void writeInternal(ByteBuffer buffer) throws IOException {
Platform.blockGuardOnNetwork();
checkOpen();
init();
// Need to loop through at least once to enable handshaking where no application
// bytes are
// processed.
int len = buffer.remaining();
SSLEngineResult engineResult;
do {
target.clear();
engineResult = engine.wrap(buffer, target);
if (engineResult.getStatus() != OK) {
throw new SSLException("Unexpected engine result " + engineResult.getStatus());
}
if (target.position() != engineResult.bytesProduced()) {
throw new SSLException("Engine bytesProduced " + engineResult.bytesProduced()
+ " does not match bytes written " + target.position());
}
len -= engineResult.bytesConsumed();
if (len != buffer.remaining()) {
throw new SSLException("Engine did not read the correct number of bytes");
}
target.flip();
// Write the data to the socket.
writeToSocket();
} while (len > 0);
}
示例5: write
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
public void write(byte[] b, int off, int len) throws IOException {
runHandshake();
try (DefaultByteBufferPool.PooledByteBuffer uncompressedPooled = DefaultByteBufferPool.WRITE_DIRECT_POOL.allocate()) {
try (DefaultByteBufferPool.PooledByteBuffer encryptedPooled = DefaultByteBufferPool.HEAP_POOL.allocate()) {
ByteBuffer buf = uncompressedPooled.getBuffer();
int toWrite = len;
int written = 0;
while (toWrite > 0) {
buf.clear();
int thisBufferAmount = Math.min(toWrite, buf.remaining());
buf.put(b, off + written, thisBufferAmount);
toWrite -= thisBufferAmount;
written += thisBufferAmount;
buf.flip();
while (buf.hasRemaining()) {
encryptedPooled.getBuffer().clear();
SSLEngineResult result = sslEngine.wrap(buf, encryptedPooled.getBuffer());
encryptedPooled.getBuffer().flip();
if (result.getStatus() == SSLEngineResult.Status.BUFFER_OVERFLOW) {
close();
throw new IOException(MESSAGES.bufferOverflow());//should never happen
} else if (result.getStatus() == SSLEngineResult.Status.BUFFER_UNDERFLOW) {
close();
throw new IOException(MESSAGES.bufferUnderflow());//should never happen
} else if (result.getStatus() == SSLEngineResult.Status.CLOSED) {
close();
throw new IOException(MESSAGES.streamIsClosed());
}
int produced = result.bytesProduced();
if (produced > 0) {
getDelegateOutputStream().write(encryptedPooled.getBuffer().array(), encryptedPooled.getBuffer().arrayOffset(), encryptedPooled.getBuffer().remaining());
}
}
}
}
}
}
示例6: read
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
@Override public int read(byte[] buf, int off, int len) throws IOException
{
if (!initialHandshakeDone ||
engine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING)
{
doHandshake();
if (handshakeException != null)
throw handshakeException;
}
if (!appBuffer.hasRemaining())
{
int x = in.read();
if (x == -1)
return -1;
inBuffer.clear();
inBuffer.put((byte) x);
inBuffer.putInt(in.readInt());
int reclen = inBuffer.getShort(3) & 0xFFFF;
in.readFully(inBuffer.array(), 5, reclen);
inBuffer.position(0).limit(reclen + 5);
synchronized (engine)
{
appBuffer.clear();
SSLEngineResult result = engine.unwrap(inBuffer, appBuffer);
Status status = result.getStatus();
if (status == Status.CLOSED && result.bytesProduced() == 0)
return -1;
}
inBuffer.compact();
appBuffer.flip();
}
int l = Math.min(len, appBuffer.remaining());
appBuffer.get(buf, off, l);
return l;
}
示例7: run
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
@Override
public void run() {
long written = 0;
try {
for (int i = offset; i < offset + length; i++) {
ByteBuffer src = srcs[i];
while (src.hasRemaining()) {
socketWriteBuffer.clear();
// Encrypt the data
SSLEngineResult r = sslEngine.wrap(src, socketWriteBuffer);
written += r.bytesConsumed();
Status s = r.getStatus();
if (s == Status.OK || s == Status.BUFFER_OVERFLOW) {
// Need to write out the bytes and may need to read from
// the source again to empty it
} else {
// Status.BUFFER_UNDERFLOW - only happens on unwrap
// Status.CLOSED - unexpected
throw new IllegalStateException(sm.getString(
"asyncChannelWrapperSecure.statusWrap"));
}
// Check for tasks
if (r.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable = sslEngine.getDelegatedTask();
while (runnable != null) {
runnable.run();
runnable = sslEngine.getDelegatedTask();
}
}
socketWriteBuffer.flip();
// Do the write
int toWrite = r.bytesProduced();
while (toWrite > 0) {
Future<Integer> f =
socketChannel.write(socketWriteBuffer);
Integer socketWrite = f.get();
toWrite -= socketWrite.intValue();
}
}
}
if (writing.compareAndSet(true, false)) {
future.complete(Long.valueOf(written));
} else {
future.fail(new IllegalStateException(sm.getString(
"asyncChannelWrapperSecure.wrongStateWrite")));
}
} catch (Exception e) {
future.fail(e);
}
}
示例8: run
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
@Override
public void run() {
long written = 0;
try {
for (int i = offset; i < offset + length; i++) {
ByteBuffer src = srcs[i];
while (src.hasRemaining()) {
socketWriteBuffer.clear();
// Encrypt the data
SSLEngineResult r = sslEngine.wrap(src, socketWriteBuffer);
written += r.bytesConsumed();
Status s = r.getStatus();
if (s == Status.OK || s == Status.BUFFER_OVERFLOW) {
// Need to write out the bytes and may need to read
// from
// the source again to empty it
} else {
// Status.BUFFER_UNDERFLOW - only happens on unwrap
// Status.CLOSED - unexpected
throw new IllegalStateException(sm.getString("asyncChannelWrapperSecure.statusWrap"));
}
// Check for tasks
if (r.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable = sslEngine.getDelegatedTask();
while (runnable != null) {
runnable.run();
runnable = sslEngine.getDelegatedTask();
}
}
socketWriteBuffer.flip();
// Do the write
int toWrite = r.bytesProduced();
while (toWrite > 0) {
Future<Integer> f = socketChannel.write(socketWriteBuffer);
Integer socketWrite = f.get();
toWrite -= socketWrite.intValue();
}
}
}
if (writing.compareAndSet(true, false)) {
future.complete(Long.valueOf(written));
} else {
future.fail(new IllegalStateException(sm.getString("asyncChannelWrapperSecure.wrongStateWrite")));
}
} catch (Exception e) {
future.fail(e);
}
}
示例9: unwrap
import javax.net.ssl.SSLEngineResult; //导入方法依赖的package包/类
private byte[] unwrap(ByteBuffer[] encryptedBuffers, SSLEngine engine) throws IOException {
ByteArrayOutputStream cleartextStream = new ByteArrayOutputStream();
int decryptedBufferSize = 8192;
final ByteBuffer encryptedBuffer = combine(encryptedBuffers);
final ByteBuffer decryptedBuffer = bufferType.newBuffer(decryptedBufferSize);
while (encryptedBuffer.hasRemaining()) {
if (!decryptedBuffer.hasRemaining()) {
decryptedBuffer.clear();
}
int prevPos = decryptedBuffer.position();
SSLEngineResult unwrapResult = engine.unwrap(encryptedBuffer, decryptedBuffer);
SSLEngineResult.Status status = unwrapResult.getStatus();
switch (status) {
case BUFFER_OVERFLOW:
case OK: {
break;
}
default: { throw new RuntimeException("Unexpected SSLEngine status: " + status); }
}
int newPos = decryptedBuffer.position();
int bytesProduced = unwrapResult.bytesProduced();
assertEquals(bytesProduced, newPos - prevPos);
// Add any generated bytes to the output stream.
if (bytesProduced > 0 || status == Status.BUFFER_OVERFLOW) {
byte[] decryptedBytes = new byte[unwrapResult.bytesProduced()];
// Read the chunk that was just written to the output array.
int limit = decryptedBuffer.limit();
decryptedBuffer.limit(newPos);
decryptedBuffer.position(prevPos);
decryptedBuffer.get(decryptedBytes);
// Restore the position and limit.
decryptedBuffer.limit(limit);
// Write the decrypted bytes to the stream.
cleartextStream.write(decryptedBytes);
}
}
return cleartextStream.toByteArray();
}