Java Code Examples for java.nio.ByteBuffer.getShort()

The following are Jave code examples for showing how to use getShort() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jchmlib   File: ChmFtsHeader.java   View Source Code Vote up 6 votes
ChmFtsHeader(ByteBuffer bb) throws IOException {
    ByteBufferHelper.skip(bb, 0x14);
    nodeOffset = bb.getInt();
    treeDepth = bb.getShort();
    ByteBufferHelper.skip(bb, 4);
    docIndexS = bb.get();
    docIndexR = bb.get();
    codeCountS = bb.get();
    codeCountR = bb.get();
    locCodesS = bb.get();
    locCodesR = bb.get();
    ByteBufferHelper.skip(bb, 10);
    nodeLen = bb.getInt();
    ByteBufferHelper.skip(bb, 12);
    maxWordLen = bb.getInt();
}
 
Example 2
Project: athena   File: Fragment.java   View Source Code Vote up 6 votes
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
    final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
    this.nextHeader = bb.get();
    bb.get();
    short sscratch = bb.getShort();
    this.fragmentOffset = (short) (sscratch >> 3 & 0x1fff);
    this.moreFragment = (byte) (sscratch & 0x1);
    this.identification = bb.getInt();

    Deserializer<? extends IPacket> deserializer;
    if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(this.nextHeader)) {
        deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(this.nextHeader);
    } else {
        deserializer = Data.deserializer();
    }
    try {
        this.payload = deserializer.deserialize(data, bb.position(),
                                                bb.limit() - bb.position());
        this.payload.setParent(this);
    } catch (DeserializationException e) {
        return this;
    }

    return this;
}
 
Example 3
Project: openjdk-jdk10   File: Type1Font.java   View Source Code Vote up 6 votes
private void verifyPFB(ByteBuffer bb) throws FontFormatException {

        int pos = 0;
        while (true) {
            try {
                int segType = bb.getShort(pos) & 0xffff;
                if (segType == 0x8001 || segType == 0x8002) {
                    bb.order(ByteOrder.LITTLE_ENDIAN);
                    int segLen = bb.getInt(pos+2);
                    bb.order(ByteOrder.BIG_ENDIAN);
                    if (segLen <= 0) {
                        throw new FontFormatException("bad segment length");
                    }
                    pos += segLen+6;
                } else if (segType == 0x8003) {
                    return;
                } else {
                    throw new FontFormatException("bad pfb file");
                }
            } catch (BufferUnderflowException bue) {
                throw new FontFormatException(bue.toString());
            } catch (Exception e) {
                throw new FontFormatException(e.toString());
            }
        }
    }
 
Example 4
Project: apkfile   File: TypeChunk.java   View Source Code Vote up 6 votes
@Nullable
private static Entry newInstance(ByteBuffer buffer, TypeChunk parent) {
    int headerSize = buffer.getShort() & 0xFFFF;
    int flags = buffer.getShort() & 0xFFFF;
    int keyIndex = buffer.getInt();
    ResourceValue value = null;
    Map<Integer, ResourceValue> values = new LinkedHashMap<>();
    int parentEntry = 0;
    if ((flags & FLAG_COMPLEX) != 0) {
        parentEntry = buffer.getInt(); //2131427792
        int valueCount = buffer.getInt();
        for (int i = 0; i < valueCount; ++i) {
            values.put(buffer.getInt(), ResourceValue.create(buffer));
        }
    } else {
        value = ResourceValue.create(buffer);
    }
    return new AutoValue_TypeChunk_Entry(headerSize, flags, keyIndex, value, values, parentEntry, parent);
}
 
Example 5
Project: jdk8u-jdk   File: TypeAnnotationParser.java   View Source Code Vote up 6 votes
private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    if (rawAnnotations == null)
        return EMPTY_TYPE_ANNOTATION_ARRAY;

    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int annotationCount = buf.getShort() & 0xFFFF;
    List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);

    // Parse each TypeAnnotation
    for (int i = 0; i < annotationCount; i++) {
         TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
         if (ta != null)
             typeAnnotations.add(ta);
    }

    return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
}
 
Example 6
Project: openjdk-jdk10   File: AnnotationParser.java   View Source Code Vote up 6 votes
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 7
Project: ConfigCenter   File: RocketMQSerializable.java   View Source Code Vote up 6 votes
public static HashMap<String, String> mapDeserialize(byte[] bytes) {
    if (bytes == null || bytes.length <= 0)
        return null;

    HashMap<String, String> map = new HashMap<String, String>();
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);

    short keySize = 0;
    byte[] keyContent = null;
    int valSize = 0;
    byte[] valContent = null;
    while (byteBuffer.hasRemaining()) {
        keySize = byteBuffer.getShort();
        keyContent = new byte[keySize];
        byteBuffer.get(keyContent);

        valSize = byteBuffer.getInt();
        valContent = new byte[valSize];
        byteBuffer.get(valContent);

        map.put(new String(keyContent, RemotingSerializable.CHARSET_UTF8), new String(valContent,
                RemotingSerializable.CHARSET_UTF8));
    }
    return map;
}
 
Example 8
Project: OpenJSharp   File: AnnotationParser.java   View Source Code Vote up 6 votes
private static Object parseIntArray(int length,
                             ByteBuffer buf, ConstantPool constPool) {
    int[] result = new  int[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'I') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 9
Project: openjdk-jdk10   File: TypeAnnotationParser.java   View Source Code Vote up 6 votes
private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    if (rawAnnotations == null)
        return EMPTY_TYPE_ANNOTATION_ARRAY;

    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int annotationCount = buf.getShort() & 0xFFFF;
    List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);

    // Parse each TypeAnnotation
    for (int i = 0; i < annotationCount; i++) {
         TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
         if (ta != null)
             typeAnnotations.add(ta);
    }

    return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
}
 
Example 10
Project: jdk8u-jdk   File: AnnotationParser.java   View Source Code Vote up 6 votes
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 11
Project: AndroidSDK2.0   File: ByteConverter.java   View Source Code Vote up 5 votes
/**
 * byte[] to short
 *
 * @param bytes array must is 1 ~ 2 bytes. little endian
 * @return short value
 */
public static short byteArrayToShort(byte[] bytes)
{
    Short newValue;
    ByteBuffer temp = ByteBuffer.wrap(bytes);
    temp.order(ByteOrder.LITTLE_ENDIAN);
    newValue = temp.getShort();
    
    return newValue;
}
 
Example 12
Project: OpenJSharp   File: AnnotationParser.java   View Source Code Vote up 5 votes
/**
 * Skips the annotation at the current position in the specified
 * byte buffer.  The cursor of the byte buffer must point to
 * an "annotation structure" OR two bytes into an annotation
 * structure (i.e., after the type index).
 *
 * @parameter complete true if the byte buffer points to the beginning
 *     of an annotation structure (rather than two bytes in).
 */
private static void skipAnnotation(ByteBuffer buf, boolean complete) {
    if (complete)
        buf.getShort();   // Skip type index
    int numMembers = buf.getShort() & 0xFFFF;
    for (int i = 0; i < numMembers; i++) {
        buf.getShort();   // Skip memberNameIndex
        skipMemberValue(buf);
    }
}
 
Example 13
Project: CodeBroker   File: DefaultSFSDataSerializer.java   View Source Code Vote up 5 votes
private DataWrapper binDecode_SHORT_ARRAY(ByteBuffer buffer) throws CodecException {
    short arraySize = this.getTypeArraySize(buffer);
    ArrayList<Short> array = new ArrayList<Short>();

    for (int j = 0; j < arraySize; ++j) {
        short shortValue = buffer.getShort();
        array.add(Short.valueOf(shortValue));
    }

    return new DataWrapper(DataType.SHORT_ARRAY, array);
}
 
Example 14
Project: L2J-Global   File: ComplexBlock.java   View Source Code Vote up 5 votes
public ComplexBlock(ByteBuffer bb)
{
	_data = new short[IBlock.BLOCK_CELLS];
	for (int cellOffset = 0; cellOffset < IBlock.BLOCK_CELLS; cellOffset++)
	{
		_data[cellOffset] = bb.getShort();
	}
}
 
Example 15
Project: AndResM   File: ResourceConfiguration.java   View Source Code Vote up 4 votes
static ResourceConfiguration create(ByteBuffer buffer) {
  int startPosition = buffer.position();  // The starting buffer position to calculate bytes read.
  int size = buffer.getInt();
  int mcc = buffer.getShort() & 0xFFFF;
  int mnc = buffer.getShort() & 0xFFFF;
  byte[] language = new byte[2];
  buffer.get(language);
  byte[] region = new byte[2];
  buffer.get(region);
  int orientation = UnsignedBytes.toInt(buffer.get());
  int touchscreen = UnsignedBytes.toInt(buffer.get());
  int density = buffer.getShort() & 0xFFFF;
  int keyboard = UnsignedBytes.toInt(buffer.get());
  int navigation = UnsignedBytes.toInt(buffer.get());
  int inputFlags = UnsignedBytes.toInt(buffer.get());
  buffer.get();  // 1 byte of padding
  int screenWidth = buffer.getShort() & 0xFFFF;
  int screenHeight = buffer.getShort() & 0xFFFF;
  int sdkVersion = buffer.getShort() & 0xFFFF;
  int minorVersion = buffer.getShort() & 0xFFFF;

  // At this point, the configuration's size needs to be taken into account as not all
  // configurations have all values.
  int screenLayout = 0;
  int uiMode = 0;
  int smallestScreenWidthDp = 0;
  int screenWidthDp = 0;
  int screenHeightDp = 0;
  byte[] localeScript = new byte[4];
  byte[] localeVariant = new byte[8];
  int screenLayout2 = 0;

  if (size >= SCREEN_CONFIG_MIN_SIZE) {
    screenLayout = UnsignedBytes.toInt(buffer.get());
    uiMode = UnsignedBytes.toInt(buffer.get());
    smallestScreenWidthDp = buffer.getShort() & 0xFFFF;
  }

  if (size >= SCREEN_DP_MIN_SIZE) {
    screenWidthDp = buffer.getShort() & 0xFFFF;
    screenHeightDp = buffer.getShort() & 0xFFFF;
  }

  if (size >= LOCALE_MIN_SIZE) {
    buffer.get(localeScript);
    buffer.get(localeVariant);
  }

  if (size >= SCREEN_CONFIG_EXTENSION_MIN_SIZE) {
    screenLayout2 = UnsignedBytes.toInt(buffer.get());
    buffer.get();  // Reserved padding
    buffer.getShort();  // More reserved padding
  }

  // After parsing everything that's known, account for anything that's unknown.
  int bytesRead = buffer.position() - startPosition;
  byte[] unknown = new byte[size - bytesRead];
  buffer.get(unknown);

  return new AutoValue_ResourceConfiguration(size, mcc, mnc, language, region, orientation,
      touchscreen, density, keyboard, navigation, inputFlags, screenWidth, screenHeight,
      sdkVersion, minorVersion, screenLayout, uiMode, smallestScreenWidthDp, screenWidthDp,
      screenHeightDp, localeScript, localeVariant, screenLayout2, unknown);
}
 
Example 16
Project: ApkMultiChannelPlugin   File: ZipUtils.java   View Source Code Vote up 4 votes
public static int getUnsignedInt16(ByteBuffer buffer) {
    return buffer.getShort() & 0xffff;
}
 
Example 17
Project: T0rlib4Android   File: Utilities.java   View Source Code Vote up 4 votes
public static Socket socks5rawSocketConnection(String networkHost, int networkPort, String socksHost, int socksPort)
        throws IOException {

    int bytesRead = 0;
    boolean end = false;
    String messageString = "";
    final byte[] messageByte = new byte[1000];

    Socket socket = new Socket();
    socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
    SocketAddress socksAddress = new InetSocketAddress(socksHost, socksPort);
    socket.connect(socksAddress, CONNECT_TIMEOUT_MILLISECONDS);

    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    outputStream.write((byte) 0x05);
    outputStream.write((byte) 0x01);
    outputStream.write((byte) 0x00);
    outputStream.write((byte) 0x01);
    outputStream.write(networkHost.getBytes());
    outputStream.writeShort((short) networkPort);

    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
    messageByte[0] = inputStream.readByte();
    messageByte[1] = inputStream.readByte();
    if (messageByte[0] != (byte) 0x05 || messageByte[1] != (byte) 0x00) {
        socket.close();
        throw new IOException("SOCKS4a connect failed, got " + messageByte[0] + " - " + messageByte[1] +
                ", but expected 0x00 - 0x5a");
    }

    ByteBuffer byteBuffer = ByteBuffer.wrap(messageByte, 0, 2);

    int bytesToRead = byteBuffer.getShort();
    LOG.info("About to read " + bytesToRead + " octets");

    while (!end) {
        bytesRead = inputStream.read(messageByte);
        messageString += new String(messageByte, 0, bytesRead);
        if (messageString.length() == bytesToRead) {
            end = true;
        }
    }

    return socket;

}
 
Example 18
Project: jdk8u-jdk   File: Type1Font.java   View Source Code Vote up 4 votes
private void verifyPFA(ByteBuffer bb) throws FontFormatException {
    if (bb.getShort() != 0x2521) { // 0x2521 is %!
        throw new FontFormatException("bad pfa font");
    }
    // remind - additional verification needed?
}
 
Example 19
Project: athena   File: UDP.java   View Source Code Vote up 4 votes
/**
 * Serializes the packet. Will compute and set the following fields if they
 * are set to specific values at the time serialize is called: -checksum : 0
 * -length : 0
 */
@Override
public byte[] serialize() {
    byte[] payloadData = null;
    if (this.payload != null) {
        this.payload.setParent(this);
        payloadData = this.payload.serialize();
    }

    this.length = (short) (8 + (payloadData == null ? 0
            : payloadData.length));

    final byte[] data = new byte[this.length];
    final ByteBuffer bb = ByteBuffer.wrap(data);

    bb.putShort((short) (this.sourcePort & 0xffff));
    bb.putShort((short) (this.destinationPort & 0xffff));
    bb.putShort(this.length);
    bb.putShort(this.checksum);
    if (payloadData != null) {
        bb.put(payloadData);
    }

    if (this.parent != null && this.parent instanceof IPv4) {
        ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP);
    }

    // compute checksum if needed
    if (this.checksum == 0) {
        bb.rewind();
        int accumulation = 0;

        // compute pseudo header mac
        if (this.parent != null) {
            if (this.parent instanceof IPv4) {
                final IPv4 ipv4 = (IPv4) this.parent;
                accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
                        + (ipv4.getSourceAddress() & 0xffff);
                accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
                        + (ipv4.getDestinationAddress() & 0xffff);
                accumulation += ipv4.getProtocol() & 0xff;
                accumulation += length & 0xffff;
            } else if (this.parent instanceof IPv6) {
                final IPv6 ipv6 = (IPv6) this.parent;
                final int bbLength =
                        Ip6Address.BYTE_LENGTH * 2 // IPv6 src, dst
                                + 2  // nextHeader (with padding)
                                + 4; // length
                final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength);
                bbChecksum.put(ipv6.getSourceAddress());
                bbChecksum.put(ipv6.getDestinationAddress());
                bbChecksum.put((byte) 0); // padding
                bbChecksum.put(ipv6.getNextHeader());
                bbChecksum.putInt(length);
                bbChecksum.rewind();
                for (int i = 0; i < bbLength / 2; ++i) {
                    accumulation += 0xffff & bbChecksum.getShort();
                }
            }
        }

        for (int i = 0; i < this.length / 2; ++i) {
            accumulation += 0xffff & bb.getShort();
        }
        // pad to an even number of shorts
        if (this.length % 2 > 0) {
            accumulation += (bb.get() & 0xff) << 8;
        }

        accumulation = (accumulation >> 16 & 0xffff)
                + (accumulation & 0xffff);
        this.checksum = (short) (~accumulation & 0xffff);
        bb.putShort(6, this.checksum);
    }
    return data;
}
 
Example 20
Project: sstore-soft   File: StoredProcedureInvocation.java   View Source Code Vote up 2 votes
/**
 * Returns the procId encoded in this invocation
 * If the procId is not set, the return value will be -1 
 * @param buffer
 * @return
 */
public static int getProcedureId(ByteBuffer buffer) {
    return (buffer.getShort(12));
}