Browse Source

Override ZipOutputStream.write(int)

master
Helder Pereira 4 years ago
parent
commit
793519b02e
4 changed files with 43 additions and 0 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      contributors.xml
  3. +18
    -0
      src/main/org/apache/tools/zip/ZipOutputStream.java
  4. +20
    -0
      src/tests/junit/org/apache/tools/zip/ZipOutputStreamTest.java

+ 1
- 0
CONTRIBUTORS View File

@@ -166,6 +166,7 @@ Grégoire Vatry
Günther Kögel
Harish Prabandham
Haroon Rafique
Helder Pereira
Hiroaki Nakamura
Holger Engels
Holger Joest


+ 4
- 0
contributors.xml View File

@@ -696,6 +696,10 @@
<first>Haroon</first>
<last>Rafique</last>
</name>
<name>
<first>Helder</first>
<last>Pereira</last>
</name>
<name>
<first>Hiroaki</first>
<last>Nakamura</last>


+ 18
- 0
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -325,6 +325,11 @@ public class ZipOutputStream extends FilterOutputStream {

private final Calendar calendarInstance = Calendar.getInstance();

/**
* Temporary buffer used for the {@link #write(int)} method.
*/
private final byte[] oneByte = new byte[1];

/**
* Creates a new ZIP OutputStream filtering the underlying stream.
* @param out the outputstream to zip
@@ -901,6 +906,19 @@ public class ZipOutputStream extends FilterOutputStream {
return ZipUtil.canHandleEntryData(ae);
}

/**
* Writes a byte to ZIP entry.
*
* @param b the byte to write
* @throws IOException on error
* @since Ant 1.10.10
*/
@Override
public void write(int b) throws IOException {
oneByte[0] = (byte) (b & 0xff);
write(oneByte, 0, 1);
}

/**
* Writes bytes to ZIP entry.
*


+ 20
- 0
src/tests/junit/org/apache/tools/zip/ZipOutputStreamTest.java View File

@@ -21,8 +21,14 @@ package org.apache.tools.zip;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipInputStream;

import static org.junit.Assert.assertEquals;

@@ -70,4 +76,18 @@ public class ZipOutputStreamTest {
ZipUtil.adjustToLong(2 * Integer.MAX_VALUE));
}

@Test
public void testWriteAndReadManifest() throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME));
new Manifest().write(zos);
}
try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ZipInputStream zis = new ZipInputStream(bais)) {
zis.getNextEntry();
zis.closeEntry();
}
}
}
}

Loading…
Cancel
Save