1 package org.apache.lucene.analysis.payloads;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Utility methods for encoding payloads.
24 public class PayloadHelper {
26 public static byte[] encodeFloat(float payload) {
27 return encodeFloat(payload, new byte[4], 0);
30 public static byte[] encodeFloat(float payload, byte[] data, int offset){
31 return encodeInt(Float.floatToIntBits(payload), data, offset);
34 public static byte[] encodeInt(int payload){
35 return encodeInt(payload, new byte[4], 0);
38 public static byte[] encodeInt(int payload, byte[] data, int offset){
39 data[offset] = (byte)(payload >> 24);
40 data[offset + 1] = (byte)(payload >> 16);
41 data[offset + 2] = (byte)(payload >> 8);
42 data[offset + 3] = (byte) payload;
48 * @see #decodeFloat(byte[], int)
49 * @see #encodeFloat(float)
50 * @return the decoded float
52 public static float decodeFloat(byte [] bytes){
53 return decodeFloat(bytes, 0);
57 * Decode the payload that was encoded using {@link #encodeFloat(float)}.
58 * NOTE: the length of the array must be at least offset + 4 long.
59 * @param bytes The bytes to decode
60 * @param offset The offset into the array.
61 * @return The float that was encoded
63 * @see #encodeFloat(float)
65 public static final float decodeFloat(byte [] bytes, int offset){
67 return Float.intBitsToFloat(decodeInt(bytes, offset));
70 public static final int decodeInt(byte [] bytes, int offset){
71 return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16)
72 | ((bytes[offset + 2] & 0xFF) << 8) | (bytes[offset + 3] & 0xFF);