GZIPOutputStream problem with my class which extending OutputStream

263 views Asked by At

I have following problem. I have written Stream class, which extends OutputStream. Here is a code:

import java.io.IOException;
import java.io.OutputStream;

public class ToUpperLetterOutputStream extends OutputStream {
    public OutputStream outputStream;

    public ToUpperLetterOutputStream(OutputStream outputStream)
    {
        this.outputStream =  outputStream;
    }

    @Override
    public void write(int b) {
        char ch  = toUpper((char)b);
        try {
            outputStream.write((int)ch);
        } catch (IOException e) {
            System.err.println("IO ERROR");
        }
    }

    protected char toUpper(int b)
    {
        char ch = (char)b;
        if (Character.isLowerCase(b))
        {
            ch = Character.toUpperCase(ch);
        }

        return ch;
    }
}

Now I'm trying to use my stream with GZIPOutputStream:

    byte[] bytes = stringBuilder.toString().getBytes();

    try (
            GZIPOutputStream g =  new GZIPOutputStream( new ToUpperLetterOutputStream(
                    new FileOutputStream("result"))))  {

                    g.write(bytes, 0, bytes.length);
                    g.finish();

    } catch (FileNotFoundException e) {
        System.err.println("Nie znaleziono pliku");
    } catch (IOException e) {
        System.err.println("IO Error");
    }

Program works fine, but when I am trying to decompress my result file, I'm getting following exception:

java.util.zip.ZipException: invalid literal/lengths set
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at Test.decompressGzipFile(Test.java:36)
at Test.main(Test.java:50)

When I am not using my Stream, decompression works fine. Why is that happening and what should I change to make this code correct?

0

There are 0 answers