Java Reader Read loadResAsString(String res)

Here you can find the source of loadResAsString(String res)

Description

load Res As String

License

Open Source License

Declaration

static public String loadResAsString(String res) throws IOException 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.Reader;

public class Main {
    static public String loadResAsString(String res) throws IOException {
        InputStream is = loadRes(res);
        return getStreamContentAsString(is, "UTF-8");
    }/*from  w  w w  .  java 2 s.  com*/

    static public InputStream loadRes(String res) throws IOException {
        InputStream is = null;
        if (res.startsWith("file:")) {
            is = new FileInputStream(res.substring("file:".length()));
        } else if (res.startsWith("classpath:")) {
            res = res.substring("classpath:".length());
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(res);
        } else {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(res);
        }
        return is;
    }

    static public String getStreamContentAsString(InputStream is, String encoding) throws IOException {
        return new String(getStreamContentAsBytes(is), encoding);
    }

    static public String getStreamContentAsString(Reader in) throws IOException {
        char[] data = new char[4912];
        int available = -1;
        StringBuilder b = new StringBuilder();
        while ((available = in.read(data)) > -1) {
            b.append(data, 0, available);
        }
        return b.toString();
    }

    static public byte[] getStreamContentAsBytes(InputStream is) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1;
        while ((available = buffer.read(data)) > -1) {
            output.write(data, 0, available);
        }
        is.close();
        return output.toByteArray();
    }

    static public byte[] getStreamContentAsBytes(InputStream is, int maxRead) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1, read = 0;
        while ((available = buffer.read(data)) > -1 && read < maxRead) {
            if (maxRead - read < available)
                available = maxRead - read;
            output.write(data, 0, available);
            read += available;
        }
        return output.toByteArray();
    }
}

Related

  1. load(InputStream input, int bufsize)
  2. loadChars(Reader in)
  3. loadFully(String path)
  4. loadReader(final Reader in, final StringBuffer buffer)
  5. loadReader(java.io.Reader in)
  6. loadText(Reader reader, int length)