Java 8 Base64 流包装器吃掉最后两个字符?
Java 8 Base64 stream wrapper eats last two chars?
我有以下代码:
public static String encode(Object object)
{
final String result;
try (ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Output output = new Output(bOut))
{
KRYO.writeObject(output, object);
output.flush();
result = Base64.getUrlEncoder().encodeToString(bOut.toByteArray());
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return result;
}
public static String encodeTest(Object object)
{
final String result;
try (ByteArrayOutputStream bOut = new ByteArrayOutputStream();
OutputStream base64Out = Base64.getUrlEncoder().wrap(bOut);
Output output = new Output(base64Out))
{
KRYO.writeObject(output, object);
output.flush();
result = bOut.toString();
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return result;
}
测试:
String test = "asdf";
System.out.println(encode(test));
System.out.println(encodeTest(test));
给出输出(使用 kryo 记录器):
00:10 TRACE: [kryo] Write: asdf
00:10 TRACE: [kryo] Object graph complete.
YXNk5g==
00:10 TRACE: [kryo] Write: asdf
00:10 TRACE: [kryo] Object graph complete.
YXNk
所以我的代码有错误吗?
我是否滥用了包装器?
是不是kryo的错误(用3.0.3和4.0.0版本测试过)?
根据 Base64.Encoder.wrap()
的文档,您需要在完成后关闭流:
It is recommended to promptly close the returned output stream after use, during which it will flush all possible leftover bytes to the underlying output stream.
在编码器流关闭后调用 bOut.toString()
将解决问题:
public static String encodeTest(Object object)
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try (OutputStream base64Out = Base64.getUrlEncoder().wrap(bOut);
Output output = new Output(base64Out))
{
KRYO.writeObject(output, object);
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return bOut.toString();
}
我有以下代码:
public static String encode(Object object)
{
final String result;
try (ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Output output = new Output(bOut))
{
KRYO.writeObject(output, object);
output.flush();
result = Base64.getUrlEncoder().encodeToString(bOut.toByteArray());
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return result;
}
public static String encodeTest(Object object)
{
final String result;
try (ByteArrayOutputStream bOut = new ByteArrayOutputStream();
OutputStream base64Out = Base64.getUrlEncoder().wrap(bOut);
Output output = new Output(base64Out))
{
KRYO.writeObject(output, object);
output.flush();
result = bOut.toString();
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return result;
}
测试:
String test = "asdf";
System.out.println(encode(test));
System.out.println(encodeTest(test));
给出输出(使用 kryo 记录器):
00:10 TRACE: [kryo] Write: asdf
00:10 TRACE: [kryo] Object graph complete.
YXNk5g==
00:10 TRACE: [kryo] Write: asdf
00:10 TRACE: [kryo] Object graph complete.
YXNk
所以我的代码有错误吗?
我是否滥用了包装器?
是不是kryo的错误(用3.0.3和4.0.0版本测试过)?
根据 Base64.Encoder.wrap()
的文档,您需要在完成后关闭流:
It is recommended to promptly close the returned output stream after use, during which it will flush all possible leftover bytes to the underlying output stream.
在编码器流关闭后调用 bOut.toString()
将解决问题:
public static String encodeTest(Object object)
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try (OutputStream base64Out = Base64.getUrlEncoder().wrap(bOut);
Output output = new Output(base64Out))
{
KRYO.writeObject(output, object);
} catch (IOException e)
{
// Do nothing, should never happen!
throw new RuntimeException("MEGA FAIL");
}
return bOut.toString();
}