GZIPOutputStream problem with my class which extending OutputStream
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?
java outputstream extending gzipoutputstream
|
show 1 more comment
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?
java outputstream extending gzipoutputstream
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 exampleGZIPOutputStream
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 toBufferedOutputStream 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 toflush()
andclose()
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
|
show 1 more comment
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?
java outputstream extending gzipoutputstream
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?
java outputstream extending gzipoutputstream
java outputstream extending gzipoutputstream
asked Nov 21 '18 at 10:18
MikolajMGTMikolajMGT
407
407
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 exampleGZIPOutputStream
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 toBufferedOutputStream 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 toflush()
andclose()
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
|
show 1 more comment
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 exampleGZIPOutputStream
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 toBufferedOutputStream 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 toflush()
andclose()
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
What did you expect to happens? You corrupt stream.
– talex
Nov 21 '18 at 10:25
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
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
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
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
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
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
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
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409846%2fgzipoutputstream-problem-with-my-class-which-extending-outputstream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409846%2fgzipoutputstream-problem-with-my-class-which-extending-outputstream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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()
andclose()
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