0

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?

MikolajMGT
  • 113
  • 2
  • 10
  • What did you expect to happens? You corrupt stream. – talex Nov 21 '18 at 10:25
  • I do not quite understand why this operation is corrupting stream. If I extend OutputStream and only change small letters to big, what is a reason of corruption of stream? – MikolajMGT Nov 21 '18 at 10:30
  • 1
    In you example `GZIPOutputStream` store data using your stream. Try to zip some file. Open it in editor and replace small letters to big. Then try unzip. – talex Nov 21 '18 at 10:38
  • Okay, so I builded streams in wrong order. I changed it to `BufferedOutputStream g = new BufferedOutputStream(new ToUpperLetterOutputStream( new GZIPOutputStream(new FileOutputStream("result")))))` But now I am not able to call finish() method from GZIPOutputStream, which also corrupts my result file. Is there any way to handle it? – MikolajMGT Nov 21 '18 at 10:52
  • 1
    You should forward calls to `flush()` and `close()` to your underlying OutputStream. The best way would be to extend FilterOutputStream, which is made exactly for such situation. – vanje Nov 21 '18 at 11:00
  • Okay, thanks for answers :) – MikolajMGT Nov 21 '18 at 13:17

0 Answers0