Here you can find the source of getResourceAsStream(Class> aClass, String name)
private static ImageInputStream getResourceAsStream(Class<?> aClass, String name) throws IOException
//package com.java2s; /*//w w w. ja v a 2 s. c o m * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 3 of the License, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, see http://www.gnu.org/licenses/ */ import javax.imageio.stream.FileCacheImageInputStream; import javax.imageio.stream.ImageInputStream; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; public class Main { private static ImageInputStream getResourceAsStream(Class<?> aClass, String name) throws IOException { final InputStream is = aClass.getResourceAsStream(name); if (is == null) { throw new IOException(MessageFormat.format( "resource {0} not found", name)); } return new FileCacheImageInputStream(is, null); } }