1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.latin.utils;
18 
19 import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.DictBuffer;
20 
21 /**
22  * This class provides an implementation for the FusionDictionary buffer interface that is backed
23  * by a simpled byte array. It allows to create a binary dictionary in memory.
24  */
25 public final class ByteArrayDictBuffer implements DictBuffer {
26     private byte[] mBuffer;
27     private int mPosition;
28 
ByteArrayDictBuffer(final byte[] buffer)29     public ByteArrayDictBuffer(final byte[] buffer) {
30         mBuffer = buffer;
31         mPosition = 0;
32     }
33 
34     @Override
readUnsignedByte()35     public int readUnsignedByte() {
36         return mBuffer[mPosition++] & 0xFF;
37     }
38 
39     @Override
readUnsignedShort()40     public int readUnsignedShort() {
41         final int retval = readUnsignedByte();
42         return (retval << 8) + readUnsignedByte();
43     }
44 
45     @Override
readUnsignedInt24()46     public int readUnsignedInt24() {
47         final int retval = readUnsignedShort();
48         return (retval << 8) + readUnsignedByte();
49     }
50 
51     @Override
readInt()52     public int readInt() {
53         final int retval = readUnsignedShort();
54         return (retval << 16) + readUnsignedShort();
55     }
56 
57     @Override
position()58     public int position() {
59         return mPosition;
60     }
61 
62     @Override
position(int position)63     public void position(int position) {
64         mPosition = position;
65     }
66 
67     @Override
put(final byte b)68     public void put(final byte b) {
69         mBuffer[mPosition++] = b;
70     }
71 
72     @Override
limit()73     public int limit() {
74         return mBuffer.length - 1;
75     }
76 
77     @Override
capacity()78     public int capacity() {
79         return mBuffer.length;
80     }
81 }
82