Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.util; |
michael@0 | 6 | |
michael@0 | 7 | import android.content.Context; |
michael@0 | 8 | import android.content.res.Resources; |
michael@0 | 9 | |
michael@0 | 10 | import java.io.IOException; |
michael@0 | 11 | import java.io.InputStream; |
michael@0 | 12 | import java.io.InputStreamReader; |
michael@0 | 13 | import java.io.StringWriter; |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * {@code RawResource} provides API to load raw resources in different |
michael@0 | 17 | * forms. For now, we only load them as strings. We're using raw resources |
michael@0 | 18 | * as localizable 'assets' as opposed to a string that can be directly |
michael@0 | 19 | * translatable e.g. JSON file vs string. |
michael@0 | 20 | * |
michael@0 | 21 | * This is just a utility class to avoid code duplication for the different |
michael@0 | 22 | * cases where need to read such assets. |
michael@0 | 23 | */ |
michael@0 | 24 | public final class RawResource { |
michael@0 | 25 | public static String getAsString(Context context, int id) throws IOException { |
michael@0 | 26 | InputStreamReader reader = null; |
michael@0 | 27 | |
michael@0 | 28 | try { |
michael@0 | 29 | final Resources res = context.getResources(); |
michael@0 | 30 | final InputStream is = res.openRawResource(id); |
michael@0 | 31 | if (is == null) { |
michael@0 | 32 | return null; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | reader = new InputStreamReader(is); |
michael@0 | 36 | |
michael@0 | 37 | final char[] buffer = new char[1024]; |
michael@0 | 38 | final StringWriter s = new StringWriter(); |
michael@0 | 39 | |
michael@0 | 40 | int n; |
michael@0 | 41 | while ((n = reader.read(buffer, 0, buffer.length)) != -1) { |
michael@0 | 42 | s.write(buffer, 0, n); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | return s.toString(); |
michael@0 | 46 | } finally { |
michael@0 | 47 | if (reader != null) { |
michael@0 | 48 | reader.close(); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | } |