下面列出了java.nio.ByteBuffer#arrayOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Decodes all bytes from the input byte buffer using the {@link Base64}
* encoding scheme, writing the results into a newly-allocated ByteBuffer.
*
* <p> Upon return, the source buffer's position will be updated to
* its limit; its limit will not have been changed. The returned
* output buffer's position will be zero and its limit will be the
* number of resulting decoded bytes
*
* <p> {@code IllegalArgumentException} is thrown if the input buffer
* is not in valid Base64 encoding scheme. The position of the input
* buffer will not be advanced in this case.
*
* @param buffer
* the ByteBuffer to decode
*
* @return A newly-allocated byte buffer containing the decoded bytes
*
* @throws IllegalArgumentException
* if {@code src} is not in valid Base64 scheme.
*/
public ByteBuffer decode(ByteBuffer buffer) {
int pos0 = buffer.position();
try {
byte[] src;
int sp, sl;
if (buffer.hasArray()) {
src = buffer.array();
sp = buffer.arrayOffset() + buffer.position();
sl = buffer.arrayOffset() + buffer.limit();
buffer.position(buffer.limit());
} else {
src = new byte[buffer.remaining()];
buffer.get(src);
sp = 0;
sl = src.length;
}
byte[] dst = new byte[outLength(src, sp, sl)];
return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
} catch (IllegalArgumentException iae) {
buffer.position(pos0);
throw iae;
}
}
/**
* Update the digest using the specified ByteBuffer. The digest is
* updated using the {@code input.remaining()} bytes starting
* at {@code input.position()}.
* Upon return, the buffer's position will be equal to its limit;
* its limit will not have changed.
*
* @param input the ByteBuffer
* @since 1.5
*/
protected void engineUpdate(ByteBuffer input) {
if (input.hasRemaining() == false) {
return;
}
if (input.hasArray()) {
byte[] b = input.array();
int ofs = input.arrayOffset();
int pos = input.position();
int lim = input.limit();
engineUpdate(b, ofs + pos, lim - pos);
input.position(lim);
} else {
int len = input.remaining();
int n = JCAUtil.getTempArraySize(len);
if ((tempArray == null) || (n > tempArray.length)) {
tempArray = new byte[n];
}
while (len > 0) {
int chunk = Math.min(len, tempArray.length);
input.get(tempArray, 0, chunk);
engineUpdate(tempArray, 0, chunk);
len -= chunk;
}
}
}
public void copy(ByteBuffer srcBuf, int srcPosition, ByteBuffer trgBuf, int trgPosition, int length)
{
Object src;
long srcOffset;
if (srcBuf.hasArray())
{
src = srcBuf.array();
srcOffset = BYTE_ARRAY_BASE_OFFSET + srcBuf.arrayOffset();
}
else
{
src = null;
srcOffset = theUnsafe.getLong(srcBuf, DIRECT_BUFFER_ADDRESS_OFFSET);
}
copy(src, srcOffset + srcPosition, trgBuf, trgPosition, length);
}
/**
* Decodes all bytes from the input byte buffer using the {@link Base64}
* encoding scheme, writing the results into a newly-allocated ByteBuffer.
*
* <p> Upon return, the source buffer's position will be updated to
* its limit; its limit will not have been changed. The returned
* output buffer's position will be zero and its limit will be the
* number of resulting decoded bytes
*
* <p> {@code IllegalArgumentException} is thrown if the input buffer
* is not in valid Base64 encoding scheme. The position of the input
* buffer will not be advanced in this case.
*
* @param buffer
* the ByteBuffer to decode
*
* @return A newly-allocated byte buffer containing the decoded bytes
*
* @throws IllegalArgumentException
* if {@code buffer} is not in valid Base64 scheme
*/
public ByteBuffer decode(ByteBuffer buffer) {
int pos0 = buffer.position();
try {
byte[] src;
int sp, sl;
if (buffer.hasArray()) {
src = buffer.array();
sp = buffer.arrayOffset() + buffer.position();
sl = buffer.arrayOffset() + buffer.limit();
buffer.position(buffer.limit());
} else {
src = new byte[buffer.remaining()];
buffer.get(src);
sp = 0;
sl = src.length;
}
byte[] dst = new byte[decodedOutLength(src, sp, sl)];
return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
} catch (IllegalArgumentException iae) {
buffer.position(pos0);
throw iae;
}
}
/**
* Decodes all bytes from the input byte buffer using the {@link Base64}
* encoding scheme, writing the results into a newly-allocated ByteBuffer.
* <p> Upon return, the source buffer's position will be updated to
* its limit; its limit will not have been changed. The returned
* output buffer's position will be zero and its limit will be the
* number of resulting decoded bytes
* <p> {@code IllegalArgumentException} is thrown if the input buffer
* is not in valid Base64 encoding scheme. The position of the input
* buffer will not be advanced in this case.
*
* @param buffer the ByteBuffer to decode
* @return A newly-allocated byte buffer containing the decoded bytes
* @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme.
*/
public ByteBuffer decode(ByteBuffer buffer) {
int pos0 = buffer.position();
try {
byte[] src;
int sp, sl;
if (buffer.hasArray()) {
src = buffer.array();
sp = buffer.arrayOffset() + buffer.position();
sl = buffer.arrayOffset() + buffer.limit();
buffer.position(buffer.limit());
} else {
src = new byte[buffer.remaining()];
buffer.get(src);
sp = 0;
sl = src.length;
}
byte[] dst = new byte[outLength(src, sp, sl)];
return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
} catch (IllegalArgumentException iae) {
buffer.position(pos0);
throw iae;
}
}
private static void uncheckedZeros(final ByteBuffer buffer, int offset, int bytes) {
if (buffer.isReadOnly()) {
throw new ReadOnlyBufferException();
}
final byte zero = (byte) 0;
if (buffer.isDirect() && PlatformDependent.hasUnsafe()) {
PlatformDependent.setMemory(PlatformDependent.directBufferAddress(buffer) + offset, bytes, zero);
} else if (buffer.hasArray()) {
//SIMD OPTIMIZATION
final int arrayOffset = buffer.arrayOffset();
final int start = arrayOffset + offset;
Arrays.fill(buffer.array(), start, start + bytes, zero);
} else {
//slow path
for (int i = 0; i < bytes; i++) {
buffer.put(i + offset, zero);
}
}
}
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());
}
}
@Override
public void send(MTSPacket packet) throws IOException {
ByteBuffer buffer = packet.getBuffer();
Preconditions.checkArgument(buffer.hasArray());
DatagramPacket datagramPacket = new DatagramPacket(buffer.array(), buffer.arrayOffset(), buffer.limit(), inetSocketAddress);
multicastSocket.send(datagramPacket);
}
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
char c = sa[sp];
if (c <= '\u007f')
return CoderResult.unmappableForLength(1);
int ncode = encodeDouble(c);
if (ncode != 0 && c != '\u0000' ) {
da[dp++] = (byte) ((ncode >> 8) & 0x7f);
da[dp++] = (byte) (ncode & 0x7f);
sp++;
continue;
}
return CoderResult.unmappableForLength(1);
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
CoderResult cr = CoderResult.UNDERFLOW;
if ((dl - dp) < (sl - sp)) {
sl = sp + (dl - dp);
cr = CoderResult.OVERFLOW;
}
try {
while (sp < sl) {
char c = sa[sp];
int b = enc.encode(c);
if (b == UNMAPPABLE_ENCODING) {
if (Character.isSurrogate(c)) {
if (sgp == null)
sgp = new Surrogate.Parser();
if (sgp.parse(c, sa, sp, sl) >= 0)
return CoderResult.unmappableForLength(2);
}
return CoderResult.unmappableForLength(1);
}
da[dp++] = (byte)b;
sp++;
}
return cr;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
@Override
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
assert (sp <= sl);
sp = (sp <= sl ? sp : sl);
byte[] 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) {
char c = sa[sp];
if (dl - dp < 1)
return CoderResult.OVERFLOW;
if (!canEncode(c))
return CoderResult.unmappableForLength(1);
sp++;
da[dp++] = table[c - 0x2700];
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
public static DeflateResult deflate(ByteBuffer input) {
byte[] inputBuf;
int inputOffset;
int inputLength = input.remaining();
if (input.hasArray()) {
inputBuf = input.array();
inputOffset = input.arrayOffset() + input.position();
input.position(input.limit());
} else {
inputBuf = new byte[inputLength];
inputOffset = 0;
input.get(inputBuf);
}
CRC32 crc32 = new CRC32();
crc32.update(inputBuf, inputOffset, inputLength);
long crc32Value = crc32.getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Deflater deflater = new Deflater(9, true);
deflater.setInput(inputBuf, inputOffset, inputLength);
deflater.finish();
byte[] buf = new byte[65536];
while (!deflater.finished()) {
int chunkSize = deflater.deflate(buf);
out.write(buf, 0, chunkSize);
}
return new DeflateResult(inputLength, crc32Value, out.toByteArray());
}
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
sp = (sp <= sl ? sp : sl);
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
dp = (dp <= dl ? dp : dl);
try {
while (sp < sl) {
int b = sa[sp];
char c = decode(b);
if (c == '\uFFFD')
return CoderResult.unmappableForLength(1);
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = c;
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
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) {
int b = sa[sp];
char c = decode(b);
if (c == '\uFFFD')
return CoderResult.unmappableForLength(1);
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = c;
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
private CoderResult encodeArrayLoop(CharBuffer src,
ByteBuffer dst)
{
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
int dlASCII = dp + Math.min(sl - sp, dl - dp);
// ASCII only loop
while (dp < dlASCII && sa[sp] < '\u0080')
da[dp++] = (byte) sa[sp++];
while (sp < sl) {
char c = sa[sp];
if (c < 0x80) {
// Have at most seven bits
if (dp >= dl)
return overflow(src, sp, dst, dp);
da[dp++] = (byte)c;
} else if (c < 0x800) {
// 2 bytes, 11 bits
if (dl - dp < 2)
return overflow(src, sp, dst, dp);
da[dp++] = (byte)(0xc0 | (c >> 6));
da[dp++] = (byte)(0x80 | (c & 0x3f));
} else if (Character.isSurrogate(c)) {
// Have a surrogate pair
if (sgp == null)
sgp = new Surrogate.Parser();
int uc = sgp.parse(c, sa, sp, sl);
if (uc < 0) {
updatePositions(src, sp, dst, dp);
return sgp.error();
}
if (dl - dp < 4)
return overflow(src, sp, dst, dp);
da[dp++] = (byte)(0xf0 | ((uc >> 18)));
da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
da[dp++] = (byte)(0x80 | ((uc >> 6) & 0x3f));
da[dp++] = (byte)(0x80 | (uc & 0x3f));
sp++; // 2 chars
} else {
// 3 bytes, 16 bits
if (dl - dp < 3)
return overflow(src, sp, dst, dp);
da[dp++] = (byte)(0xe0 | ((c >> 12)));
da[dp++] = (byte)(0x80 | ((c >> 6) & 0x3f));
da[dp++] = (byte)(0x80 | (c & 0x3f));
}
sp++;
}
updatePositions(src, sp, dst, dp);
return CoderResult.UNDERFLOW;
}
private CoderResult encodeArrayLoop(CharBuffer src,
ByteBuffer dst)
{
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
assert (sp <= sl);
sp = (sp <= sl ? sp : sl);
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
assert (dp <= dl);
dp = (dp <= dl ? dp : dl);
int outputSize = 0;
try {
char inputChar;
while (sp < sl) {
int index = Integer.MIN_VALUE;
inputChar = sa[sp];
if (inputChar >= 0x0000 && inputChar <= 0x007f) {
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = (byte) inputChar;
sp++;
continue;
}
// if inputChar == ZWJ replace it with halant
// if inputChar == ZWNJ replace it with Nukta
if (inputChar == 0x200c) {
inputChar = HALANT_CHAR;
}
else if (inputChar == 0x200d) {
inputChar = NUKTA_CHAR;
}
if (inputChar >= 0x0900 && inputChar <= 0x097f) {
index = ((int)(inputChar) - 0x0900)*2;
}
if (Character.isSurrogate(inputChar)) {
if (sgp.parse(inputChar, sa, sp, sl) < 0)
return sgp.error();
return sgp.unmappableResult();
}
if (index == Integer.MIN_VALUE ||
encoderMappingTable[index] == NO_CHAR) {
return CoderResult.unmappableForLength(1);
} else {
if(encoderMappingTable[index + 1] == NO_CHAR) {
if(dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = encoderMappingTable[index];
} else {
if(dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = encoderMappingTable[index];
da[dp++] = encoderMappingTable[index + 1];
}
sp++;
}
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
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) {
int b1, b2;
b1 = sa[sp];
int inputSize = 1;
int v = 0;
char outputChar = REPLACE_CHAR;
if (b1 < 0)
b1 += 256;
if (b1 == SO) { // Shift out
// For SO characters - simply validate the state and if OK
// update the state and go to the next byte
if (currentState != SBCS)
return CoderResult.malformedForLength(1);
else
currentState = DBCS;
} else if (b1 == SI) {
// For SI characters - simply validate the state and if OK
// update the state and go to the next byte
if (currentState != DBCS) {
return CoderResult.malformedForLength(1);
} else {
currentState = SBCS;
}
} else {
if (currentState == SBCS) {
outputChar = singleByteToChar.charAt(b1);
} else {
if (sl - sp < 2)
return CoderResult.UNDERFLOW;
b2 = sa[sp + 1];
if (b2 < 0)
b2 += 256;
inputSize++;
// Check validity of dbcs ebcdic byte pair values
if ((b1 != 0x40 || b2 != 0x40) &&
(b2 < 0x41 || b2 > 0xfe)) {
return CoderResult.malformedForLength(2);
}
// Lookup in the two level index
v = b1 * 256 + b2;
outputChar = index2.charAt(index1[((v & mask1) >> shift)]
+ (v & mask2));
}
if (outputChar == '\uFFFD')
return CoderResult.unmappableForLength(inputSize);
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = outputChar;
}
sp += inputSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
int b1 = sa[sp] & 0xff;
char c = decodeSingle(b1);
int inSize = 1, outSize = 1;
char[] cc = null;
if (c == UNMAPPABLE) {
if (sl - sp < 2)
return CoderResult.UNDERFLOW;
int b2 = sa[sp + 1] & 0xff;
c = decodeDouble(b1, b2);
inSize++;
if (c == UNMAPPABLE) {
cc = decodeDoubleEx(b1, b2);
if (cc == null) {
if (decodeSingle(b2) == UNMAPPABLE)
return CoderResult.unmappableForLength(2);
else
return CoderResult.unmappableForLength(1);
}
outSize++;
}
}
if (dl - dp < outSize)
return CoderResult.OVERFLOW;
if (outSize == 2) {
da[dp++] = cc[0];
da[dp++] = cc[1];
} else {
da[dp++] = c;
}
sp += inSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
int outputSize = 0; // size of output
try {
while (sp < sl) {
int index;
int theBytes;
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);
index = index1[((c & mask1) >> shift)]
+ (c & mask2);
if (index < 15000)
theBytes = (int)(index2.charAt(index));
else
theBytes = (int)(index2a.charAt(index-15000));
b1 = (byte)((theBytes & 0x0000ff00)>>8);
b2 = (byte)(theBytes & 0x000000ff);
if (b1 == 0x00 && b2 == 0x00
&& c != '\u0000') {
return CoderResult.unmappableForLength(1);
}
if (b1 == 0) {
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = (byte) b2;
} else {
if (dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = (byte) b1;
da[dp++] = (byte) b2;
}
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
char c = sa[sp];
if (Character.isSurrogate(c)) {
if (sgp.parse(c, sa, sp, sl) < 0)
return sgp.error();
if (sl - sp < 2)
return CoderResult.UNDERFLOW;
char c2 = sa[sp + 1];
byte[] outputBytes = new byte[2];
outputBytes = encodeSurrogate(c, c2);
if (outputBytes == null) {
return sgp.unmappableResult();
}
else {
if (dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = outputBytes[0];
da[dp++] = outputBytes[1];
sp += 2;
continue;
}
}
if (c >= '\uFFFE')
return CoderResult.unmappableForLength(1);
int b = encodeSingle(c);
if (b != -1) { // Single Byte
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = (byte)b;
sp++;
continue;
}
int ncode = encodeDouble(c);
if (ncode != 0 && c != '\u0000' ) {
if (dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = (byte) ((ncode & 0xff00) >> 8);
da[dp++] = (byte) (ncode & 0xff);
sp++;
continue;
}
return CoderResult.unmappableForLength(1);
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}