Here you can find the source of loadResource(final String s)
Parameter | Description |
---|---|
s | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] loadResource(final String s) throws IOException
//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; } } }