如何将Map <String,String>编码为Base64字符串?

Chr*_*ris 7 java encoding serialization

我喜欢将字符串的java映射编码为单个基本64位编码字符串.编码的字符串将被传输到远程端点,并且可能由不好的人操纵.因此,应该发生的最糟糕的事情是invaild key,value-tuples,但不应该带来任何其他安全风险.

例:

Map<String,String> map = ...
String encoded = Base64.encode(map);

// somewhere else
Map<String,String> map = Base64.decode(encoded);
Run Code Online (Sandbox Code Playgroud)

是的,必须是Base64.不喜欢那个 或那个 或任何其他这些.是否有现有的轻量级解决方案(Single Utils-Class首选)?或者我必须创建自己的?

有什么比这更好的?

// marshalling
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(map);
oos.close();
String encoded = new String(Base64.encodeBase64(baos.toByteArray()));

// unmarshalling 
byte[] decoded = Base64.decodeBase64(encoded.getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
ObjectInputStream ois = new ObjectInputStream(bais);
map = (Map<String,String>) ois.readObject();
ois.close();
Run Code Online (Sandbox Code Playgroud)

谢谢,

Bal*_*usC 13

我的主要要求是:编码字符串应尽可能短,并且只包含来自base64字母表的拉丁字符或字符(不是我的调用).没有其他要求.

使用Google Gson转换MapJSON.使用GZIPOutputStream压缩JSON字符串.使用Apache Commons CodecBase64Base64OutputStream将压缩字节编码为Base64字符串.

开球示例:

public static void main(String[] args) throws IOException {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    String serialized = serialize(map);
    Map<String, String> deserialized = deserialize(serialized, new TypeToken<Map<String, String>>() {}.getType());

    System.out.println(deserialized);
}

public static String serialize(Object object) throws IOException {
    ByteArrayOutputStream byteaOut = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = null;
    try {
        gzipOut = new GZIPOutputStream(new Base64OutputStream(byteaOut));
        gzipOut.write(new Gson().toJson(object).getBytes("UTF-8"));
    } finally {
        if (gzipOut != null) try { gzipOut.close(); } catch (IOException logOrIgnore) {}
    }
    return new String(byteaOut.toByteArray());
}

public static <T> T deserialize(String string, Type type) throws IOException {
    ByteArrayOutputStream byteaOut = new ByteArrayOutputStream();
    GZIPInputStream gzipIn = null;
    try {
        gzipIn = new GZIPInputStream(new Base64InputStream(new ByteArrayInputStream(string.getBytes("UTF-8"))));
        for (int data; (data = gzipIn.read()) > -1;) {
            byteaOut.write(data);
        }
    } finally {
        if (gzipIn != null) try { gzipIn.close(); } catch (IOException logOrIgnore) {}
    }
    return new Gson().fromJson(new String(byteaOut.toByteArray()), type);
}
Run Code Online (Sandbox Code Playgroud)