add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / payloads / PayloadHelper.java
1 package org.apache.lucene.analysis.payloads;
2 /**
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 /**
21  * Utility methods for encoding payloads.
22  *
23  **/
24 public class PayloadHelper {
25
26   public static byte[] encodeFloat(float payload) {
27     return encodeFloat(payload, new byte[4], 0);
28   }
29
30   public static byte[] encodeFloat(float payload, byte[] data, int offset){
31     return encodeInt(Float.floatToIntBits(payload), data, offset);
32   }
33
34   public static byte[] encodeInt(int payload){
35     return encodeInt(payload, new byte[4], 0);
36   }
37
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;
43     return data;
44   }
45
46   /**
47    * @param bytes
48    * @see #decodeFloat(byte[], int)
49    * @see #encodeFloat(float)
50    * @return the decoded float
51    */
52   public static float decodeFloat(byte [] bytes){
53     return decodeFloat(bytes, 0);
54   }
55
56   /**
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
62    *
63    * @see #encodeFloat(float)
64    */
65   public static final float decodeFloat(byte [] bytes, int offset){
66
67     return Float.intBitsToFloat(decodeInt(bytes, offset));
68   }
69
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);
73   }
74 }