Get a resource from the GATE ClassLoader as a byte array.
(String resourceName)
| 273 | /** Get a resource from the GATE ClassLoader as a byte array. |
| 274 | */ |
| 275 | public static byte[] getResourceAsByteArray(String resourceName) |
| 276 | throws IOException, IndexOutOfBoundsException, ArrayStoreException { |
| 277 | |
| 278 | InputStream resourceInputStream = getResourceAsStream(resourceName); |
| 279 | BufferedInputStream resourceStream = |
| 280 | new BufferedInputStream(resourceInputStream); |
| 281 | byte b; |
| 282 | final int bufSize = 1024; |
| 283 | byte[] buf = new byte[bufSize]; |
| 284 | int i = 0; |
| 285 | |
| 286 | // get the whole resource into buf (expanding the array as needed) |
| 287 | while( (b = (byte) resourceStream.read()) != -1 ) { |
| 288 | if(i == buf.length) { |
| 289 | byte[] newBuf = new byte[buf.length * 2]; |
| 290 | System.arraycopy (buf,0,newBuf,0,i); |
| 291 | buf = newBuf; |
| 292 | } |
| 293 | buf[i++] = b; |
| 294 | } |
| 295 | |
| 296 | // close the resource stream |
| 297 | resourceStream.close(); |
| 298 | |
| 299 | // copy the contents of buf to an array of the correct size |
| 300 | byte[] bytes = new byte[i]; |
| 301 | // copy from buf to bytes |
| 302 | System.arraycopy (buf,0,bytes,0,i); |
| 303 | return bytes; |
| 304 | } // getResourceAsByteArray(String) |
| 305 | |
| 306 | /** Get a resource from the GATE resources directory as a byte array. |
| 307 | * The resource name should be relative to <code>resourcePath</code> which |
nothing calls this directly
no test coverage detected