Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * This file is part of OpenVPN-Settings.
 *
 * Copyright  2009-2012  Friedrich Schuffelhut
 *
 * OpenVPN-Settings is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenVPN-Settings is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenVPN-Settings.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Report bugs or new features at: http://code.google.com/p/android-openvpn-settings/
 * Contact the author at:          android.openvpn@schaeuffelhut.de
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.nio.charset.Charset;

import android.content.res.AssetFileDescriptor;

import android.net.LocalSocket;
import android.util.Log;

public class Main {
    public static CharSequence getFileAsString(File file) {
        Reader reader = null;
        StringBuilder sb = new StringBuilder(1024);
        try {
            reader = new InputStreamReader(new FileInputStream(file), Charset.forName("ISO-8859-1"));
            char[] buf = new char[1024];
            int length;
            while ((length = reader.read(buf)) >= 0)
                sb.append(buf, 0, length);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(reader);
        }
        String string = sb.toString();
        return string;
    }

    public final static void closeQuietly(InputStream is) {
        try {
            if (is != null)
                is.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing InputStream", e);
        }
    }

    public final static void closeQuietly(AssetFileDescriptor afd) {
        try {
            if (afd != null)
                afd.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing AssetFileDescriptor", e);
        }
    }

    public final static void closeQuietly(Reader r) {
        try {
            if (r != null)
                r.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing InputStream", e);
        }
    }

    public final static void closeQuietly(OutputStream os) {
        try {
            if (os != null)
                os.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing OutputStream", e);
        }
    }

    public final static void closeQuietly(Writer writer) {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing Writer", e);
        }
    }

    public final static void closeQuietly(Socket s) {
        try {
            if (s != null)
                s.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing Socket", e);
        }
    }

    public final static void closeQuietly(LocalSocket s) {
        try {
            if (s != null)
                s.close();
        } catch (Exception e) {
            Log.e("OpenVPN", "closing LocalSocket", e);
        }
    }
}