How to convert java POJO class into a byte array as I wanted to save the object into a gz file in S3
I get this exception Caused by: java.io.NotSerializableException
public byte[] compressData(User user) throws IOException {
byte[] data;
try(ByteArrayOutputStream byteStream = new ByteArrayOutputStream();) {
try (GZIPOutputStream objectOutputStream = new GZIPOutputStream(byteStream);) {
try (ObjectOutputStream zipStream = new ObjectOutputStream(objectOutputStream);) {
zipStream.writeObject(user);
}
data = byteStream.toByteArray();
} catch (Exception e) {
throw new IOException(e);
}
}
return data;
}
private final Type userType = new TypeToken<User>() {}.getType();
private final Gson = new Gson();
compressData(gson.toJson(user,userType));
public static byte[] compressData(String user) throws IOException {
byte[] data;
try(ByteArrayOutputStream byteStream = new ByteArrayOutputStream();){
try(GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);){
zipStream.write(data.getBytes(StandardCharsets.UTF_8));
}
data = byteStream.toByteArray();
} catch(Exception e) {
throw new IOException("Error while compressing the User record ", e);
}
return data;
}