/platform/util/src/com/intellij/util/ImageLoader.java
https://github.com/TrevorBender/intellij-community · Java · 112 lines · 83 code · 14 blank · 15 comment · 12 complexity · 7d7c0d4313e10e95a25c652561039afc MD5 · raw file
- /*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.intellij.util;
- import com.intellij.openapi.diagnostic.Logger;
- import com.intellij.openapi.util.IconLoader;
- import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream;
- import com.intellij.util.io.URLUtil;
- import org.jetbrains.annotations.NonNls;
- import org.jetbrains.annotations.NotNull;
- import org.jetbrains.annotations.Nullable;
- import sun.reflect.Reflection;
- import javax.swing.*;
- import java.awt.*;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Serializable;
- import java.net.URL;
- @Deprecated
- public class ImageLoader implements Serializable {
- private static final Component ourComponent = new Component() {};
- private static final Logger LOG = Logger.getInstance("#com.intellij.util.ImageLoader");
- private static boolean waitForImage(Image image) {
- if (image == null) return false;
- if (image.getWidth(null) > 0) return true;
- MediaTracker mediatracker = new MediaTracker(ourComponent);
- mediatracker.addImage(image, 1);
- try {
- mediatracker.waitForID(1, 5000);
- } catch (InterruptedException ex) {
- LOG.info(ex);
- }
- return !mediatracker.isErrorID(1);
- }
- @Nullable
- public static Image loadFromResource(@NonNls String s) {
- int stackFrameCount = 2;
- Class callerClass = Reflection.getCallerClass(stackFrameCount);
- while (callerClass != null && callerClass.getClassLoader() == null) { // looks like a system class
- callerClass = Reflection.getCallerClass(++stackFrameCount);
- }
- if (callerClass == null) {
- callerClass = Reflection.getCallerClass(1);
- }
- return loadFromResource(s, callerClass);
- }
- @Nullable
- public static Image loadFromResource(String s, Class aClass) {
- final InputStream stream = aClass.getResourceAsStream(s);
- return stream != null ? loadFromStream(stream) : null;
- }
- public static Image loadFromStream(@NotNull final InputStream inputStream) {
- try {
- BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream();
- try {
- byte[] buffer = new byte[1024];
- while (true) {
- final int n = inputStream.read(buffer);
- if (n < 0) break;
- outputStream.write(buffer, 0, n);
- }
- }
- finally {
- inputStream.close();
- }
- Image image = Toolkit.getDefaultToolkit().createImage(outputStream.getInternalBuffer(), 0, outputStream.size());
- waitForImage(image);
- return image;
- } catch (Exception ex) {
- LOG.error(ex);
- }
- return null;
- }
- public static Image loadFromUrl(URL url) {
- try {
- return loadFromStream(URLUtil.openStream(url));
- }
- catch (IOException e) {
- LOG.error(e);
- return null;
- }
- }
- public static boolean isGoodSize(final Icon icon) {
- return IconLoader.isGoodSize(icon);
- }
- }