org.apache.thrift.EncodingUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of libthrift Show documentation
Show all versions of libthrift Show documentation
Thrift is a software framework for scalable cross-language
services development.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.thrift;
/**
* Utility methods for use when encoding/decoding raw data as byte arrays.
*/
public class EncodingUtils {
/**
* Encode integer
as a series of 4 bytes into buf
* starting at position 0 within that buffer.
*
* @param integer
* The integer to encode.
* @param buf
* The buffer to write to.
*/
public static final void encodeBigEndian(final int integer, final byte[] buf) {
encodeBigEndian(integer, buf, 0);
}
/**
* Encode integer
as a series of 4 bytes into buf
* starting at position offset
.
*
* @param integer
* The integer to encode.
* @param buf
* The buffer to write to.
* @param offset
* The offset within buf
to start the encoding.
*/
public static final void encodeBigEndian(final int integer, final byte[] buf, int offset) {
buf[offset] = (byte) (0xff & (integer >> 24));
buf[offset + 1] = (byte) (0xff & (integer >> 16));
buf[offset + 2] = (byte) (0xff & (integer >> 8));
buf[offset + 3] = (byte) (0xff & (integer));
}
/**
* Decode a series of 4 bytes from buf
, starting at position 0,
* and interpret them as an integer.
*
* @param buf
* The buffer to read from.
* @return An integer, as read from the buffer.
*/
public static final int decodeBigEndian(final byte[] buf) {
return decodeBigEndian(buf, 0);
}
/**
* Decode a series of 4 bytes from buf
, start at
* offset
, and interpret them as an integer.
*
* @param buf
* The buffer to read from.
* @param offset
* The offset with buf
to start the decoding.
* @return An integer, as read from the buffer.
*/
public static final int decodeBigEndian(final byte[] buf, int offset) {
return ((buf[offset] & 0xff) << 24) | ((buf[offset + 1] & 0xff) << 16)
| ((buf[offset + 2] & 0xff) << 8) | ((buf[offset + 3] & 0xff));
}
}