/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

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.util;
  17. import com.intellij.openapi.diagnostic.Logger;
  18. import com.intellij.openapi.util.IconLoader;
  19. import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream;
  20. import com.intellij.util.io.URLUtil;
  21. import org.jetbrains.annotations.NonNls;
  22. import org.jetbrains.annotations.NotNull;
  23. import org.jetbrains.annotations.Nullable;
  24. import sun.reflect.Reflection;
  25. import javax.swing.*;
  26. import java.awt.*;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.Serializable;
  30. import java.net.URL;
  31. @Deprecated
  32. public class ImageLoader implements Serializable {
  33. private static final Component ourComponent = new Component() {};
  34. private static final Logger LOG = Logger.getInstance("#com.intellij.util.ImageLoader");
  35. private static boolean waitForImage(Image image) {
  36. if (image == null) return false;
  37. if (image.getWidth(null) > 0) return true;
  38. MediaTracker mediatracker = new MediaTracker(ourComponent);
  39. mediatracker.addImage(image, 1);
  40. try {
  41. mediatracker.waitForID(1, 5000);
  42. } catch (InterruptedException ex) {
  43. LOG.info(ex);
  44. }
  45. return !mediatracker.isErrorID(1);
  46. }
  47. @Nullable
  48. public static Image loadFromResource(@NonNls String s) {
  49. int stackFrameCount = 2;
  50. Class callerClass = Reflection.getCallerClass(stackFrameCount);
  51. while (callerClass != null && callerClass.getClassLoader() == null) { // looks like a system class
  52. callerClass = Reflection.getCallerClass(++stackFrameCount);
  53. }
  54. if (callerClass == null) {
  55. callerClass = Reflection.getCallerClass(1);
  56. }
  57. return loadFromResource(s, callerClass);
  58. }
  59. @Nullable
  60. public static Image loadFromResource(String s, Class aClass) {
  61. final InputStream stream = aClass.getResourceAsStream(s);
  62. return stream != null ? loadFromStream(stream) : null;
  63. }
  64. public static Image loadFromStream(@NotNull final InputStream inputStream) {
  65. try {
  66. BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream();
  67. try {
  68. byte[] buffer = new byte[1024];
  69. while (true) {
  70. final int n = inputStream.read(buffer);
  71. if (n < 0) break;
  72. outputStream.write(buffer, 0, n);
  73. }
  74. }
  75. finally {
  76. inputStream.close();
  77. }
  78. Image image = Toolkit.getDefaultToolkit().createImage(outputStream.getInternalBuffer(), 0, outputStream.size());
  79. waitForImage(image);
  80. return image;
  81. } catch (Exception ex) {
  82. LOG.error(ex);
  83. }
  84. return null;
  85. }
  86. public static Image loadFromUrl(URL url) {
  87. try {
  88. return loadFromStream(URLUtil.openStream(url));
  89. }
  90. catch (IOException e) {
  91. LOG.error(e);
  92. return null;
  93. }
  94. }
  95. public static boolean isGoodSize(final Icon icon) {
  96. return IconLoader.isGoodSize(icon);
  97. }
  98. }