From ed380ea5d0201e2e75eada6be54fd55399877810 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 12 Nov 2004 12:15:13 +0000 Subject: [PATCH] deprecated or not, this is the only way to avoid creation of both - a Date and a Calendar instance - that works on JDK 1.3 and 1.2 - Calendar#setTimeInMillis has been protected before 1.4 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277028 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/bzip2/CBZip2InputStream.java | 8 +++--- .../org/apache/tools/zip/ZipOutputStream.java | 27 ++++++++----------- .../apache/tools/zip/ZipOutputStreamTest.java | 18 +++++++------ 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/org/apache/tools/bzip2/CBZip2InputStream.java b/src/main/org/apache/tools/bzip2/CBZip2InputStream.java index afbf399d7..a226b39e5 100644 --- a/src/main/org/apache/tools/bzip2/CBZip2InputStream.java +++ b/src/main/org/apache/tools/bzip2/CBZip2InputStream.java @@ -126,7 +126,7 @@ public class CBZip2InputStream extends InputStream implements BZip2Constants { private int computedBlockCRC, computedCombinedCRC; int i2, count, chPrev, ch2; - int i, tPos; + int global_i, tPos; int rNToGo = 0; int rTPos = 0; int j2; @@ -668,14 +668,14 @@ public class CBZip2InputStream extends InputStream implements BZip2Constants { char ch; cftab[0] = 0; - for (i = 1; i <= 256; i++) { + for (int i = 1; i <= 256; i++) { cftab[i] = unzftab[i - 1]; } - for (i = 1; i <= 256; i++) { + for (int i = 1; i <= 256; i++) { cftab[i] += cftab[i - 1]; } - for (i = 0; i <= last; i++) { + for (int i = 0; i <= last; i++) { ch = (char) ll8[i]; tt[cftab[ch]] = i; cftab[ch]++; diff --git a/src/main/org/apache/tools/zip/ZipOutputStream.java b/src/main/org/apache/tools/zip/ZipOutputStream.java index 0d3c0ebdb..0f0c77f5c 100644 --- a/src/main/org/apache/tools/zip/ZipOutputStream.java +++ b/src/main/org/apache/tools/zip/ZipOutputStream.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; -import java.util.Calendar; import java.util.Date; import java.util.Hashtable; import java.util.Vector; @@ -581,7 +580,7 @@ public class ZipOutputStream extends FilterOutputStream { written += 2; // last mod. time and date - writeOut(toDosTime(new Date(ze.getTime()))); + writeOut(toDosTime(ze.getTime())); written += 4; // CRC @@ -669,7 +668,7 @@ public class ZipOutputStream extends FilterOutputStream { written += 2; // last mod. time and date - writeOut(toDosTime(new Date(ze.getTime()))); + writeOut(toDosTime(ze.getTime())); written += 4; // CRC @@ -765,12 +764,10 @@ public class ZipOutputStream extends FilterOutputStream { /** * Convert a Date object to a DOS date/time field. * - *

Stolen from InfoZip's fileio.c

- * * @since 1.1 */ protected static ZipLong toDosTime(Date time) { - return new ZipLong(toDosTime(time)); + return new ZipLong(toDosTime(time.getTime())); } /** @@ -780,21 +777,19 @@ public class ZipOutputStream extends FilterOutputStream { * * @since 1.26 */ - protected static byte[] toDosTime(Date time) { - Calendar cal = Calendar.getInstance(); - cal.setTime(time); - int year = cal.get(Calendar.YEAR); + protected static byte[] toDosTime(long t) { + Date time = new Date(t); + int year = time.getYear() + 1900; if (year < 1980) { return DOS_TIME_MIN.getBytes(); } - int month = cal.get(Calendar.MONTH) + 1; + int month = time.getMonth() + 1; long value = ((year - 1980) << 25) | (month << 21) - | (cal.get(Calendar.DAY_OF_MONTH) << 16) - | (cal.get(Calendar.HOUR_OF_DAY) << 11) - | (cal.get(Calendar.MINUTE) << 5) - | (cal.get(Calendar.SECOND) >> 1); - + | (time.getDate() << 16) + | (time.getHours() << 11) + | (time.getMinutes() << 5) + | (time.getSeconds() >> 1); byte[] result = new byte[4]; result[0] = (byte) ((value & 0xFF)); result[1] = (byte) ((value & 0xFF00) >> 8); diff --git a/src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java b/src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java index 01e720df6..fa92f858e 100644 --- a/src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java +++ b/src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java @@ -21,6 +21,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import java.util.Calendar; import java.util.Date; public class ZipOutputStreamTest extends TestCase { @@ -37,22 +38,23 @@ public class ZipOutputStreamTest extends TestCase { protected void setUp() throws Exception { time = new Date(); - byte[] result = new byte[4]; - int year = time.getYear() + 1900; - int month = time.getMonth() + 1; + Calendar cal = Calendar.getInstance(); + cal.setTime(time); + int year = cal.get(Calendar.YEAR); + int month = cal.get(Calendar.MONTH) + 1; long value = ((year - 1980) << 25) | (month << 21) - | (time.getDate() << 16) - | (time.getHours() << 11) - | (time.getMinutes() << 5) - | (time.getSeconds() >> 1); + | (cal.get(Calendar.DAY_OF_MONTH) << 16) + | (cal.get(Calendar.HOUR_OF_DAY) << 11) + | (cal.get(Calendar.MINUTE) << 5) + | (cal.get(Calendar.SECOND) >> 1); + byte[] result = new byte[4]; result[0] = (byte) ((value & 0xFF)); result[1] = (byte) ((value & 0xFF00) >> 8); result[2] = (byte) ((value & 0xFF0000) >> 16); result[3] = (byte) ((value & 0xFF000000L) >> 24); zl = new ZipLong(result); - } protected void tearDown() throws Exception {