Java Resource Get loadResource(final String s)

Here you can find the source of loadResource(final String s)

Description

Method loadResource

License

Open Source License

Parameter

Parameter Description
s a parameter

Exception

Parameter Description
IOException an exception

Return

TODO: Finish documentation

Declaration

public static byte[] loadResource(final String s) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww  w  . j  a  v a 2 s .  c o  m
 * Copyright (c) 2003 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;

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

import java.io.RandomAccessFile;

import java.net.URL;

public class Main {
    /** Size of block used in IO (4096 bytes) */
    static final int CHUNK_SIZE = 4096;
    /** Empty byte array */
    static final byte NO_BYTES[] = new byte[0];

    /**
     * Method loadResource
     *
     * @param s
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static byte[] loadResource(final String s) throws IOException {
        final URL url = new URL(s);
        final InputStream inputstream = url.openStream();
        final byte data[] = readFully(inputstream);
        inputstream.close();

        return data;
    }

    /**
     * Method readFully
     *
     * @param file
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static byte[] readFully(final File file) throws IOException {
        final RandomAccessFile randomaccessfile = new RandomAccessFile(file, "r");
        final byte bytes[] = new byte[(int) randomaccessfile.length()];
        randomaccessfile.readFully(bytes);
        randomaccessfile.close();

        return bytes;
    }

    /**
     * Method readFully
     *
     * @param inputstream
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static byte[] readFully(final InputStream inputstream) throws IOException {
        final byte bytes[] = new byte[CHUNK_SIZE];
        final ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();

        do {
            final int i = inputstream.read(bytes, 0, bytes.length);

            if (i != -1) {
                bytearrayoutputstream.write(bytes, 0, i);
            } else {
                return bytearrayoutputstream.toByteArray();
            }
        } while (true);
    }

    /**
     * Method readFully
     *
     * @param inputstream
     * @param bytes
     * @param i
     * @param j
     *
     * @throws IOException
     */
    public static void readFully(final InputStream inputstream, final byte bytes[], final int i, final int j)
            throws IOException {
        int l;

        for (int k = 0; k < j; k += l) {
            l = inputstream.read(bytes, i + k, j - k);

            if (l < 0) {
                throw new EOFException(String.valueOf(
                        (new StringBuffer("expected ")).append(j).append(" bytes of content, got ").append(k)));
            }
        }
    }

    /**
     * Method readFully
     *
     * @param inputstream
     * @param i
     *
     * @return TODO: Finish documentation
     *
     * @throws IOException
     */
    public static byte[] readFully(final InputStream inputstream, final int i) throws IOException {
        if (i <= 0) {
            return NO_BYTES;
        } else {
            final byte bytes[] = new byte[i];
            readFully(inputstream, bytes, 0, i);

            return bytes;
        }
    }
}

Related

  1. getResourceString(ResourceBundle rb, String key, Object param1)
  2. getResourceString(String key)
  3. getResourceString(String key, Object... args)
  4. loadResource(Class contextClass, String resourceName)
  5. loadResource(final String name)
  6. readResource(String resource, Class c)
  7. readToString(final Class nearClass, final String resource)
  8. readToString(final Class nearClass, final String resource, final String encoding)