Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;
import android.support.annotation.RawRes;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {
    public static String getStringFromResources(final Context context, @RawRes final int rawResource) {
        final InputStream is = context.getResources().openRawResource(rawResource);
        try {
            return readStringFromInputStreamAndCloseStream(is, 4048);
        } catch (final IOException e) {
            // TODO: bad.
            throw new IllegalStateException("ain't nobody got time to handle this properly");
        }
    }

    public static String readStringFromInputStreamAndCloseStream(final InputStream inputStream,
            final int bufferSize) throws IOException {
        if (bufferSize <= 0) {
            // Safe close: it's more important to alert the programmer of
            // their error than to let them catch and continue on their way.
            throw new IllegalArgumentException("Expected buffer size larger than 0. Got: " + bufferSize);
        }

        final StringBuilder stringBuilder = new StringBuilder(bufferSize);
        final InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        try {
            int charsRead;
            final char[] buffer = new char[bufferSize];
            while ((charsRead = reader.read(buffer, 0, bufferSize)) != -1) {
                stringBuilder.append(buffer, 0, charsRead);
            }
        } finally {
            reader.close();
        }
        return stringBuilder.toString();
    }
}