Java Resource Get readResource(String resource, Class c)

Here you can find the source of readResource(String resource, Class c)

Description

_more_

License

Open Source License

Parameter

Parameter Description
resource _more_
c _more_

Return

_more_

Declaration

public static byte[] readResource(String resource, Class c) 

Method Source Code

//package com.java2s;
/**//from  w w w  .  ja va2s  .  co m
* Copyright (c) 2008-2015 Geode Systems LLC
* This Software is licensed under the Geode Systems RAMADDA License available in the source distribution in the file 
* ramadda_license.txt. The above copyright notice shall be included in all copies or substantial portions of the Software.
*/

import java.io.*;
import java.net.*;

public class Main {
    /**
     * _more_
     *
     * @param resource _more_
     * @param c _more_
     *
     * @return _more_
     */
    public static byte[] readResource(String resource, Class c) {
        return readResource(resource, c, false);
    }

    /**
     * _more_
     *
     * @param resource _more_
     * @param c _more_
     * @param printError _more_
     *
     * @return _more_
     */
    public static byte[] readResource(String resource, Class c, boolean printError) {
        try {
            InputStream inputStream = null;
            System.err.println("read resource:" + resource);
            //            IfcApplet.debug ("read resource:" + resource);
            if (!resource.startsWith("http:")) {
                inputStream = c.getResourceAsStream(resource);
            }
            if (inputStream == null) {
                URL url = new URL(resource);
                System.err.println("read url:" + url);
                //                IfcApplet.debug ("   read url:" + url);
                URLConnection connection = url.openConnection();
                inputStream = connection.getInputStream();
            }
            byte[] buffer = new byte[100000];
            byte[] bytes = new byte[10];
            int totalBytes = 0;
            while (true) {
                int howMany = inputStream.read(buffer);
                if (howMany < 0) {
                    break;
                }
                while ((howMany + totalBytes) > bytes.length) {
                    byte[] tmp = bytes;
                    bytes = new byte[tmp.length * 2];
                    System.arraycopy(tmp, 0, bytes, 0, totalBytes);
                }
                System.arraycopy(buffer, 0, bytes, totalBytes, howMany);
                totalBytes += howMany;
            }
            byte[] finalBytes = new byte[totalBytes];
            System.arraycopy(bytes, 0, finalBytes, 0, totalBytes);

            return finalBytes;
        } catch (Exception exc) {
            if (printError) {
                System.err.println("Error reading resource:" + resource + " " + exc);
                exc.printStackTrace();
            }
        }

        return null;
    }
}

Related

  1. getResourceString(String key)
  2. getResourceString(String key, Object... args)
  3. loadResource(Class contextClass, String resourceName)
  4. loadResource(final String name)
  5. loadResource(final String s)
  6. readToString(final Class nearClass, final String resource)
  7. readToString(final Class nearClass, final String resource, final String encoding)