Override ZipOutputStream.write(int)#145
Conversation
| */ | ||
| @Override | ||
| public void write(int b) throws IOException { | ||
| byte[] buf = new byte[1]; |
There was a problem hiding this comment.
We could allocate a one-byte array and reuse that for all calls. Of course then we'd need to think about thread safety issues. So maybe that's not wort the trouble given that so far nobody seems to have used that method anyway :-)
There was a problem hiding this comment.
Agreed, I thought about that but was wondering why DeflaterOutputStream didn't do it. I did it now as this class is not thread-safe anyway, and this allocation on every write may keep the GC unnecessarily busy. I checked Apache Commons Compress since you mentioned it and it's also how it's done there: https://github.com/apache/commons-compress/blob/master/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java#L52
|
Many thanks - interesting this has been there for more then a decade. Most likely this is because Ant is supposed to be the only consumer of the class and doesn't use the single byte write method itself. For general use I'd recommend to use @helfper please add yourself to the contributors.xml and CONTRIBUTORS files in Ant's top level. |
0e857f9 to
793519b
Compare
|
@bodewig I've updated the contributors files now. I've found the issue in https://github.com/johnrengelman/shadow which uses Ant's |
|
Ant predates Commons Compress by several years. Actually Ant's core doesn't have any dependencies at all, this has always been the case. Initially the XML parser had to be provided before JAXP became part of the standard class library. This is an important goal of Ant. We can bootstrap Ant with just a JDK and nothing else. |
java.util.zip.ZipOutputStreamextends fromjava.util.zip.DeflaterOutputStreamwhich overridesvoid write(int b)to redirect its calls tovoid write(byte[] b, int off, int len).org.apache.tools.zip.ZipOutputStreamdoesn't extend the same class and currently doesn't overridevoid write(int b). This means that calls to this method end up inFilterOutputStream.write(int b), which in turn passes them through to the underlyingOutputStream. This bypasses all the logic inZipOutputStream.write(byte[] b, int offset, int len)(deflation and whatnot) which produces corrupted ZIP files.One example where the method
void write(int b)is ultimately invoked is by callingjava.util.jar.Manifest.write(zipOutputStream).This PR fixes this by overriding the method
void write(int b)inorg.apache.tools.zip.ZipOutputStreamin the same way asjava.util.zip.DeflaterOutputStreamdoes.