1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.dictionarypack;
18 
19 import java.io.File;
20 
21 /**
22  * Immutable class to hold the address of an asset.
23  * As opposed to a normal file, an asset is usually represented as a contiguous byte array in
24  * the package file. Open it correctly thus requires the name of the package it is in, but
25  * also the offset in the file and the length of this data. This class encapsulates these three.
26  */
27 final class AssetFileAddress {
28     public final String mFilename;
29     public final long mOffset;
30     public final long mLength;
31 
AssetFileAddress(final String filename, final long offset, final long length)32     public AssetFileAddress(final String filename, final long offset, final long length) {
33         mFilename = filename;
34         mOffset = offset;
35         mLength = length;
36     }
37 
38     /**
39      * Makes an AssetFileAddress. This may return null.
40      *
41      * @param filename the filename.
42      * @return the address, or null if the file does not exist or the parameters are not valid.
43      */
makeFromFileName(final String filename)44     public static AssetFileAddress makeFromFileName(final String filename) {
45         if (null == filename) return null;
46         final File f = new File(filename);
47         if (!f.isFile()) return null;
48         return new AssetFileAddress(filename, 0l, f.length());
49     }
50 
51     /**
52      * Makes an AssetFileAddress. This may return null.
53      *
54      * @param filename the filename.
55      * @param offset the offset.
56      * @param length the length.
57      * @return the address, or null if the file does not exist or the parameters are not valid.
58      */
makeFromFileNameAndOffset(final String filename, final long offset, final long length)59     public static AssetFileAddress makeFromFileNameAndOffset(final String filename,
60             final long offset, final long length) {
61         if (null == filename) return null;
62         final File f = new File(filename);
63         if (!f.isFile()) return null;
64         return new AssetFileAddress(filename, offset, length);
65     }
66 }
67