private void readUtf8_slow(int charCount, int charIndex) { char[] chars = this.chars; byte[] buffer = this.buffer; while (charIndex < charCount) { if (position == limit) require(1); int b = buffer[position++] & 0xFF; switch (b >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: chars[charIndex] = (char) b; break; case 12: case 13: if (position == limit) require(1); chars[charIndex] = (char) ((b & 0x1F) << 6 | buffer[position++] & 0x3F); break; case 14: require(2); chars[charIndex] = (char) ((b & 0x0F) << 12 | (buffer[position++] & 0x3F) << 6 | buffer[position++] & 0x3F); break; } charIndex++; } }
private int readInt_slow(boolean optimizePositive) { // The buffer is guaranteed to have at least 1 byte. int b = buffer[position++]; int result = b & 0x7F; if ((b & 0x80) != 0) { require(1); byte[] buffer = this.buffer; b = buffer[position++]; result |= (b & 0x7F) << 7; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 14; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 21; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 28; } } } } return optimizePositive ? result : ((result >>> 1) ^ -(result & 1)); }
private int readUtf8Length_slow(int b) { int result = b & 0x3F; // Mask all but first 6 bits. if ((b & 0x40) != 0) { // Bit 7 means another byte, bit 8 means UTF8. require(1); byte[] buffer = this.buffer; b = buffer[position++]; result |= (b & 0x7F) << 6; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 13; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 20; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 27; } } } } return result; }
private String readAscii_slow() { position--; // Re-read the first byte. // Copy chars currently in buffer. int charCount = limit - position; if (charCount > chars.length) chars = new char[charCount * 2]; char[] chars = this.chars; byte[] buffer = this.buffer; for (int i = position, ii = 0, n = limit; i < n; i++, ii++) chars[ii] = (char) buffer[i]; position = limit; // Copy additional chars one by one. while (true) { require(1); int b = buffer[position++]; if (charCount == chars.length) { char[] newChars = new char[charCount * 2]; System.arraycopy(chars, 0, newChars, 0, charCount); chars = newChars; this.chars = newChars; } if ((b & 0x80) == 0x80) { chars[charCount++] = (char) (b & 0x7F); break; } chars[charCount++] = (char) b; } return new String(chars, 0, charCount); }
/** Reads a 4 byte int. */ public int readInt() throws KryoException { require(4); byte[] buffer = this.buffer; int position = this.position; this.position = position + 4; return (buffer[position] & 0xFF) << 24 // | (buffer[position + 1] & 0xFF) << 16 // | (buffer[position + 2] & 0xFF) << 8 // | buffer[position + 3] & 0xFF; }
/** Discards the specified number of bytes. */ public void skip(int count) throws KryoException { int skipCount = Math.min(limit - position, count); while (true) { position += skipCount; count -= skipCount; if (count == 0) break; skipCount = Math.min(count, capacity); require(skipCount); } }
/** Reads an 8 byte long. */ public long readLong() throws KryoException { require(8); byte[] buffer = this.buffer; return (long) buffer[position++] << 56 // | (long) (buffer[position++] & 0xFF) << 48 // | (long) (buffer[position++] & 0xFF) << 40 // | (long) (buffer[position++] & 0xFF) << 32 // | (long) (buffer[position++] & 0xFF) << 24 // | (buffer[position++] & 0xFF) << 16 // | (buffer[position++] & 0xFF) << 8 // | buffer[position++] & 0xFF; }
/** Reads count bytes and writes them to the specified byte[], starting at offset. */ public void readBytes(byte[] bytes, int offset, int count) throws KryoException { if (bytes == null) throw new IllegalArgumentException("bytes cannot be null."); int copyCount = Math.min(limit - position, count); while (true) { System.arraycopy(buffer, position, bytes, offset, copyCount); position += copyCount; count -= copyCount; if (count == 0) break; offset += copyCount; copyCount = Math.min(count, capacity); require(copyCount); } }
private long readLong_slow(boolean optimizePositive) { // The buffer is guaranteed to have at least 1 byte. int b = buffer[position++]; long result = b & 0x7F; if ((b & 0x80) != 0) { require(1); byte[] buffer = this.buffer; b = buffer[position++]; result |= (b & 0x7F) << 7; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 14; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (b & 0x7F) << 21; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (long) (b & 0x7F) << 28; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (long) (b & 0x7F) << 35; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (long) (b & 0x7F) << 42; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (long) (b & 0x7F) << 49; if ((b & 0x80) != 0) { require(1); b = buffer[position++]; result |= (long) b << 56; } } } } } } } } if (!optimizePositive) result = (result >>> 1) ^ -(result & 1); return result; }
/** Reads a 2 byte char. */ public char readChar() throws KryoException { require(2); return (char) (((buffer[position++] & 0xFF) << 8) | (buffer[position++] & 0xFF)); }
/** Reads a 1 byte boolean. */ public boolean readBoolean() throws KryoException { require(1); return buffer[position++] == 1; }
/** Reads a 2 byte short as an int from 0 to 65535. */ public int readShortUnsigned() throws KryoException { require(2); return ((buffer[position++] & 0xFF) << 8) | (buffer[position++] & 0xFF); }
/** Reads a 2 byte short. */ public short readShort() throws KryoException { require(2); return (short) (((buffer[position++] & 0xFF) << 8) | (buffer[position++] & 0xFF)); }
/** Reads a byte as an int from 0 to 255. */ public int readByteUnsigned() throws KryoException { require(1); return buffer[position++] & 0xFF; }
/** Reads a single byte. */ public byte readByte() throws KryoException { require(1); return buffer[position++]; }