1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
4 import java.io.StreamCorruptedException;
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 * A simple stream decoder which can decode values encoded with
25 * {@link SimpleIntEncoder}.
27 * @lucene.experimental
29 public class SimpleIntDecoder extends IntDecoder {
32 * reusable buffer - allocated only once as this is not a thread-safe object
34 private byte[] buffer = new byte[4];
37 public long decode() throws IOException {
39 // we need exactly 4 bytes to decode an int in this decoder impl, otherwise, throw an exception
42 int nRead = in.read(buffer, offset, 4 - offset);
45 throw new StreamCorruptedException(
46 "Need 4 bytes for decoding an int, got only " + offset);
53 int v = buffer[3] & 0xff;
54 v |= (buffer[2] << 8) & 0xff00;
55 v |= (buffer[1] << 16) & 0xff0000;
56 v |= (buffer[0] << 24) & 0xff000000;
62 public String toString() {