package io.snowcamp.papaya.doc;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

public interface Document extends Map<String, Object> {
  // a map of key/value pairs, keys are String, values are any JSON values
  
  default <T> Optional<T> get(String key, Class<T> type) {
    return Optional.ofNullable(get(key)).filter(type::isInstance).map(type::cast);
  }
  
  default OptionalInt getInt(String key) {
    return get(key, Integer.class).map(OptionalInt::of).orElseGet(OptionalInt::empty);
  }
  default OptionalLong getLong(String key) {
    return get(key, Long.class).map(OptionalLong::of).orElseGet(OptionalLong::empty);
  }
  default OptionalDouble getDouble(String key) {
    return get(key, Double.class).map(OptionalDouble::of).orElseGet(OptionalDouble::empty);
  }
  
  default void putBlob(String key, byte[] blob) {
    put(key, new String(Base64.getEncoder().encode(blob), StandardCharsets.ISO_8859_1));
  }
  default Optional<byte[]> getBlob(String key) {
    return get(key, String.class).map(base64 -> Base64.getDecoder().decode(base64));
  }
}

@@ -26,16 +26,83 @@
 a.doc;%0A%0A
+import java.io.IOException;%0Aimport java.io.UncheckedIOException;%0A//
 import j
@@ -135,16 +135,18 @@
 arsets;%0A
+//
 import j
@@ -1077,25 +1077,23 @@
 new 
-String(Base64.get
+sun.misc.BASE64
 Enco
@@ -1114,38 +1114,8 @@
 lob)
-, StandardCharsets.ISO_8859_1)
 );%0A 
@@ -1218,18 +1218,56 @@
  -%3E 
-Base64.get
+%7B%0A      try %7B%0A        return new sun.misc.BASE64
 Deco
@@ -1282,16 +1282,22 @@
 code
+Buffer
 (base64)
 );%0A 
@@ -1292,16 +1292,106 @@
 (base64)
+;%0A      %7D catch (IOException e) %7B%0A        throw new UncheckedIOException(e);%0A      %7D%0A    %7D
 );%0A  %7D%0A%7D

revert to use sun.misc.BASE64Decoder/BASE64Encoder