I have the following code which we use to compress Strings (with error and resource handling removed for clarity):
import java.util.zip.GZIP*;
import java.io.*;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
import com.Ostermiller.util.Base64;
//Code to compress the string
ByteArrayOutputStream output = new ByteArrayOutputStream(65536);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new GZIPOutputStream(output)));
writer.write(stringContents);
String compressedString = new String(Base64.encode(output.toByteArray()));
...
//Code to decompress the string
byte[] compressedData = Base64.decode(compressedString.getBytes());
BufferedInputStream reader = new BufferedInputStream(
new GZIPInputStream(new ByteArrayInputStream(compressedData)));
String uncompressedString = IOUtils.toString(reader, "UTF-8");
We are encountering an error when trying to encode and then decode strings with a ‘£’ in them. Specifically, the string compresses OK, but when trying to decompress the string we get the following stack trace:
sun.io.MalformedInputException
at sun.io.ByteToCharUTF8.convert(ByteToCharUTF8.java(Compiled Code))
at sun.nio.cs.StreamDecoder$ConverterSD.convertInto(StreamDecoder.java:287)
at sun.nio.cs.StreamDecoder$ConverterSD.implRead(StreamDecoder.java:337)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:223)
at java.io.InputStreamReader.read(InputStreamReader.java:208)
at java.io.Reader.read(Reader.java:113)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1128)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1104)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1078)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:382)
Can anyone tell me the error of my ways and how I might fix this situation? Is there a better way to be doing this? Many thanks in advance.
You should specify the character encoding when you compress the data:
If you don’t, text is converted to bytes according to the system default character encoding, which in your case is not UTF-8.