com.intellij.util.Base64 Java Examples

The following examples show how to use com.intellij.util.Base64. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomIconLoader.java    From intellij-extra-icons-plugin with MIT License 6 votes vote down vote up
public static ImageWrapper fromBase64(String base64, IconType iconType) {
    byte[] decodedBase64 = Base64.decode(base64);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodedBase64);
    Image image = null;
    try {
        switch (iconType) {
            case SVG:
                image = SVGLoader.load(byteArrayInputStream, 1.0f);
                break;
            case IMG:
                image = ImageLoader.loadFromStream(byteArrayInputStream);
                break;
        }
    } catch (IOException ex) {
        LOGGER.info("Can't load " + iconType + " icon: " + ex.getMessage(), ex);
        return null;
    }
    if (image == null) {
        return null;
    }
    return new ImageWrapper(iconType, scaleImage(image), decodedBase64);
}
 
Example #2
Source File: URLUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts byte array from given data:URL string.
 * data:URL will be decoded from base64 if it contains the marker of base64 encoding.
 *
 * @param dataUrl data:URL-like string (may be quoted)
 * @return extracted byte array or {@code null} if it cannot be extracted.
 */
@Nullable
public static byte[] getBytesFromDataUri(@Nonnull String dataUrl) {
  Matcher matcher = DATA_URI_PATTERN.matcher(StringUtil.unquoteString(dataUrl));
  if (matcher.matches()) {
    try {
      String content = matcher.group(4);
      return ";base64".equalsIgnoreCase(matcher.group(3))
             ? Base64.decode(content)
             : content.getBytes(CharsetToolkit.UTF8_CHARSET);
    }
    catch (IllegalArgumentException e) {
      return null;
    }
  }
  return null;
}
 
Example #3
Source File: CustomIconLoader.java    From intellij-extra-icons-plugin with MIT License 5 votes vote down vote up
public static String toBase64(ImageWrapper imageWrapper) {
    String base64 = null;
    IconType iconType = imageWrapper.getIconType();
    switch (iconType) {
        case SVG:
            base64 = Base64.encode(imageWrapper.getImageAsByteArray());
            break;
        case IMG:
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try {
                Image image = imageWrapper.getImage();
                if (image instanceof JBHiDPIScaledImage) {
                    image = ((JBHiDPIScaledImage) image).getDelegate();
                }
                if (image instanceof ToolkitImage) {
                    image = ((ToolkitImage) image).getBufferedImage();
                }
                if (!(image instanceof RenderedImage)) {
                    BufferedImage bufferedImage = UIUtil.createImage(
                        GRAPHICS_CFG,
                        image.getWidth(null),
                        image.getHeight(null),
                        BufferedImage.TYPE_INT_RGB,
                        PaintUtil.RoundingMode.ROUND);
                    bufferedImage.getGraphics().drawImage(image, 0, 0, null);
                    image = bufferedImage;
                }
                ImageIO.write((RenderedImage) image, "png", outputStream);
            } catch (IOException ex) {
                LOGGER.info("Can't load " + iconType + " icon: " + ex.getMessage(), ex);
            }
            base64 = Base64.encode(outputStream.toByteArray());
            break;
    }
    return base64;
}
 
Example #4
Source File: InMemoryPreferenceNode.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getByteArray(String key, byte[] def) {
  final String value = this.get(key, null);
  try {
    return Base64.decode(value);
  } catch (Exception ex) {
    return def;
  }
}
 
Example #5
Source File: InMemoryPreferenceNode.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void putByteArray(String key, byte[] value) {
  this.put(key, Base64.encode(value));
}