本文整理汇总了Java中java.nio.ByteBuffer.limit方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.limit方法的具体用法?Java ByteBuffer.limit怎么用?Java ByteBuffer.limit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.limit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: exploreHandshake
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static SSLCapabilities exploreHandshake(
ByteBuffer input, byte recordMajorVersion,
byte recordMinorVersion, int recordLength) throws IOException {
// What is the handshake type?
byte handshakeType = input.get();
if (handshakeType != 0x01) { // 0x01: client_hello message
throw new IllegalStateException("Not initial handshaking");
}
// What is the handshake body length?
int handshakeLength = getInt24(input);
// Theoretically, a single handshake message might span multiple
// records, but in practice this does not occur.
if (handshakeLength > (recordLength - 4)) { // 4: handshake header size
throw new SSLException("Handshake message spans multiple records");
}
input = input.duplicate();
input.limit(handshakeLength + input.position());
return exploreClientHello(input,
recordMajorVersion, recordMinorVersion);
}
示例2: write
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Write string to stream
*
* @param value
* @return this stream
* @throws IOException
*/
public RequestOutputStream write(final String value) throws IOException {
final ByteBuffer bytes = encoder.encode(CharBuffer.wrap(value));
super.write(bytes.array(), 0, bytes.limit());
return this;
}
示例3: decodeArrayLoop
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private CoderResult decodeArrayLoop(ByteBuffer src,
CharBuffer dst)
{
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
assert (sp <= sl);
sp = (sp <= sl ? sp : sl);
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
assert (dp <= dl);
dp = (dp <= dl ? dp : dl);
try {
while (sp < sl) {
byte b = sa[sp];
if (dp >= dl)
return CoderResult.OVERFLOW;
da[dp++] = (char)(b & 0xff);
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
示例4: send
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
throws IOException
{
if (src instanceof DirectBuffer)
return sendFromNativeBuffer(fd, src, target);
// Substitute a native buffer
int pos = src.position();
int lim = src.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
try {
bb.put(src);
bb.flip();
// Do not update src until we see how many bytes were written
src.position(pos);
int n = sendFromNativeBuffer(fd, bb, target);
if (n > 0) {
// now update src
src.position(pos + n);
}
return n;
} finally {
Util.releaseTemporaryDirectBuffer(bb);
}
}
示例5: encodeArrayLoop
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private CoderResult encodeArrayLoop(CharBuffer src,
ByteBuffer dst)
{
char[] sa = src.array();
int soff = src.arrayOffset();
int sp = soff + src.position();
int sl = soff + src.limit();
assert (sp <= sl);
sp = (sp <= sl ? sp : sl);
byte[] da = dst.array();
int doff = dst.arrayOffset();
int dp = doff + dst.position();
int dl = doff + dst.limit();
assert (dp <= dl);
dp = (dp <= dl ? dp : dl);
int dlen = dl - dp;
int slen = sl - sp;
int len = (dlen < slen) ? dlen : slen;
try {
int ret = encodeISOArray(sa, sp, da, dp, len);
sp = sp + ret;
dp = dp + ret;
if (ret != len) {
if (sgp.parse(sa[sp], sa, sp, sl) < 0)
return sgp.error();
return sgp.unmappableResult();
}
if (len < slen)
return CoderResult.OVERFLOW;
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - soff);
dst.position(dp - doff);
}
}
示例6: encodeArrayLoop
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
sp = (sp <= sl ? sp : sl);
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
dp = (dp <= dl ? dp : dl);
try {
while (sp < sl) {
char c = sa[sp];
if (Surrogate.is(c)) {
if (sgp.parse(c, sa, sp, sl) < 0)
return sgp.error();
return sgp.unmappableResult();
}
if (c >= '\uFFFE')
return CoderResult.unmappableForLength(1);
if (dl - dp < 1)
return CoderResult.OVERFLOW;
char e = index2.charAt(index1[(c & mask1) >> shift]
+ (c & mask2));
// If output byte is zero because input char is zero
// then character is mappable, o.w. fail
if (e == '\u0000' && c != '\u0000')
return CoderResult.unmappableForLength(1);
sp++;
da[dp++] = (byte)e;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
示例7: readFullyAt
import java.nio.ByteBuffer; //导入方法依赖的package包/类
final long readFullyAt(byte[] buf, int off, long len, long pos)
throws IOException
{
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.position(off);
bb.limit((int)(off + len));
return readFullyAt(bb, pos);
}
示例8: bufferData
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void bufferData(ByteBuffer data)
{
this.bindBuffer();
OpenGlHelper.glBufferData(OpenGlHelper.GL_ARRAY_BUFFER, data, 35044);
this.unbindBuffer();
this.count = data.limit() / this.vertexFormat.getNextOffset();
}
示例9: getBytesNullTerminated
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static byte[] getBytesNullTerminated(String value, String encoding) throws UnsupportedEncodingException {
Charset cs = findCharset(encoding);
ByteBuffer buf = cs.encode(value);
int encodedLen = buf.limit();
byte[] asBytes = new byte[encodedLen + 1];
buf.get(asBytes, 0, encodedLen);
asBytes[encodedLen] = 0;
return asBytes;
}
示例10: hashCode
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public @Override int hashCode() {
ByteBuffer clone = master.duplicate();
clone.position(offset+8);
clone.limit(offset+8+master.getChar(offset+6));
int code = 53*master.getChar(offset);
while (clone.hasRemaining()) code = code*53 + clone.get();
return code;
}
示例11: readBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
byte[] readBytes(int offset, int length) {
ByteBuffer buffer = readBlock(offset, length);
if (buffer.hasArray()) {
return buffer.array();
} else {
byte[] bufferBytes = new byte[buffer.limit()];
buffer.get(bufferBytes);
return bufferBytes;
}
}
示例12: main
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void main(Provider p) throws Exception {
if (p.getService("MessageDigest", "MD5") == null) {
System.out.println("Provider does not support MD5, skipping");
return;
}
Random random = new Random();
int n = 10 * 1024;
byte[] t = new byte[n];
random.nextBytes(t);
MessageDigest md = MessageDigest.getInstance("MD5", p);
byte[] d1 = md.digest(t);
// test 1: ByteBuffer with an accessible backing array
ByteBuffer b1 = ByteBuffer.allocate(n + 256);
b1.position(random.nextInt(256));
b1.limit(b1.position() + n);
ByteBuffer b2 = b1.slice();
b2.put(t);
b2.clear();
byte[] d2 = digest(md, b2, random);
if (Arrays.equals(d1, d2) == false) {
throw new Exception("Test 1 failed");
}
// test 2: direct ByteBuffer
ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);
b3.put(t);
b3.clear();
byte[] d3 = digest(md, b3, random);
if (Arrays.equals(d1, d2) == false) {
throw new Exception("Test 2 failed");
}
// test 3: ByteBuffer without an accessible backing array
b2.clear();
ByteBuffer b4 = b2.asReadOnlyBuffer();
byte[] d4 = digest(md, b4, random);
if (Arrays.equals(d1, d2) == false) {
throw new Exception("Test 3 failed");
}
System.out.println("All tests passed");
}
示例13: deserialize
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public IPacket deserialize(byte[] data, int offset, int length)
throws PacketParsingException {
ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
short sscratch;
this.version = bb.get();
this.headerLength = (byte) (this.version & 0xf);
this.version = (byte) ((this.version >> 4) & 0xf);
if (this.version != 4) {
throw new PacketParsingException(
"Invalid version for IPv4 packet: " +
this.version);
}
this.diffServ = bb.get();
this.totalLength = bb.getShort();
this.identification = bb.getShort();
sscratch = bb.getShort();
this.flags = (byte) ((sscratch >> IPV4_FLAGS_SHIFT) & IPV4_FLAGS_MASK);
this.fragmentOffset = (short) (sscratch & IPV4_OFFSET_MASK);
this.ttl = bb.get();
this.protocol = IpProtocol.of(U8.f(bb.get()));
this.checksum = bb.getShort();
this.sourceAddress = IPv4Address.of(bb.getInt());
this.destinationAddress = IPv4Address.of(bb.getInt());
if (this.headerLength > 5) {
int optionsLength = (this.headerLength - 5) * 4;
this.options = new byte[optionsLength];
bb.get(this.options);
}
IPacket payload;
isFragment = ((this.flags & IPV4_FLAGS_DONTFRAG) == 0) &&
((this.flags & IPV4_FLAGS_MOREFRAG) != 0 ||
this.fragmentOffset != 0);
if (!isFragment && IPv4.protocolClassMap.containsKey(this.protocol)) {
Class<? extends IPacket> clazz = IPv4.protocolClassMap.get(this.protocol);
try {
payload = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException("Error parsing payload for IPv4 packet", e);
}
} else {
if (log.isTraceEnabled() && isFragment) {
log.trace("IPv4 fragment detected {}->{}, forward using IP header only",
this.sourceAddress.toString(),
this.destinationAddress.toString());
}
payload = new Data();
}
int payloadLength = this.totalLength - this.headerLength * 4;
int remLength = bb.limit()-bb.position();
if (remLength < payloadLength)
payloadLength = bb.limit()-bb.position();
this.payload = payload.deserialize(data, bb.position(), payloadLength);
this.payload.setParent(this);
if (this.totalLength > length)
this.isTruncated = true;
else
this.isTruncated = false;
return this;
}
示例14: WsPongMessage
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public WsPongMessage(ByteBuffer applicationData) {
byte[] dst = new byte[applicationData.limit()];
applicationData.get(dst);
this.applicationData = ByteBuffer.wrap(dst);
}
示例15: combination_15
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void combination_15(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
// prepare ByteBuffer1 to test
ByteBuffer buf1 = ByteBuffer.allocateDirect(AAD.length / 2);
buf1.put(AAD, 0, AAD.length / 2);
buf1.position(0);
buf1.limit(AAD.length / 2);
// process the first half of AAD data
c.updateAAD(buf1);
// prepare ByteBuffer2 to test
ByteBuffer buf2 = ByteBuffer.allocateDirect(
AAD.length - AAD.length / 2);
buf2.put(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
buf2.position(0);
buf2.limit(AAD.length - AAD.length / 2);
// process the rest of AAD data
c.updateAAD(buf2);
// prepare buffers to encrypt/decrypt
ByteBuffer in = ByteBuffer.allocateDirect(plainText.length);
in.put(plainText);
in.position(0);
in.limit(plainText.length);
ByteBuffer output = ByteBuffer.allocateDirect(
c.getOutputSize(in.limit()));
output.position(0);
output.limit(c.getOutputSize(in.limit()));
// process input text
c.doFinal(in, output);
int resultSize = output.position();
byte[] result15 = new byte[resultSize];
output.position(0);
output.limit(resultSize);
output.get(result15, 0, resultSize);
results.add(result15);
}