1 /*
2  * Copyright (C) 2019 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 package com.android.libcore.timezone.telephonylookup;
17 
18 import com.android.libcore.timezone.telephonylookup.proto.TelephonyLookupProtoFile;
19 import com.google.protobuf.TextFormat;
20 
21 import java.io.BufferedReader;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.text.ParseException;
25 
26 /**
27  * A class containing utility methods for dealing with TelephonyLookupProtoFile objects.
28  */
29 public final class TelephonyLookupProtoFileSupport {
30 
TelephonyLookupProtoFileSupport()31     private TelephonyLookupProtoFileSupport() {}
32 
parseTelephonyLookupTextFile(String file)33     public static TelephonyLookupProtoFile.TelephonyLookup parseTelephonyLookupTextFile(String file)
34             throws IOException, ParseException {
35         try (BufferedReader fileReader = new BufferedReader(new FileReader(file))) {
36             TelephonyLookupProtoFile.TelephonyLookup.Builder builder =
37                     TelephonyLookupProtoFile.TelephonyLookup.newBuilder();
38             TextFormat.getParser().merge(fileReader, builder);
39             return builder.build();
40         } catch (TextFormat.ParseException e) {
41             ParseException e2 = new ParseException("Error reading proto file: " + file, 0);
42             e2.initCause(e);
43             throw e2;
44         }
45     }
46 }
47