Browse Source

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
master
Stefan Bodewig 20 years ago
parent
commit
ed380ea5d0
3 changed files with 25 additions and 28 deletions
  1. +4
    -4
      src/main/org/apache/tools/bzip2/CBZip2InputStream.java
  2. +11
    -16
      src/main/org/apache/tools/zip/ZipOutputStream.java
  3. +10
    -8
      src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java

+ 4
- 4
src/main/org/apache/tools/bzip2/CBZip2InputStream.java View File

@@ -126,7 +126,7 @@ public class CBZip2InputStream extends InputStream implements BZip2Constants {
private int computedBlockCRC, computedCombinedCRC; private int computedBlockCRC, computedCombinedCRC;


int i2, count, chPrev, ch2; int i2, count, chPrev, ch2;
int i, tPos;
int global_i, tPos;
int rNToGo = 0; int rNToGo = 0;
int rTPos = 0; int rTPos = 0;
int j2; int j2;
@@ -668,14 +668,14 @@ public class CBZip2InputStream extends InputStream implements BZip2Constants {
char ch; char ch;


cftab[0] = 0; cftab[0] = 0;
for (i = 1; i <= 256; i++) {
for (int i = 1; i <= 256; i++) {
cftab[i] = unzftab[i - 1]; cftab[i] = unzftab[i - 1];
} }
for (i = 1; i <= 256; i++) {
for (int i = 1; i <= 256; i++) {
cftab[i] += cftab[i - 1]; cftab[i] += cftab[i - 1];
} }


for (i = 0; i <= last; i++) {
for (int i = 0; i <= last; i++) {
ch = (char) ll8[i]; ch = (char) ll8[i];
tt[cftab[ch]] = i; tt[cftab[ch]] = i;
cftab[ch]++; cftab[ch]++;


+ 11
- 16
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -24,7 +24,6 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Vector; import java.util.Vector;
@@ -581,7 +580,7 @@ public class ZipOutputStream extends FilterOutputStream {
written += 2; written += 2;


// last mod. time and date // last mod. time and date
writeOut(toDosTime(new Date(ze.getTime())));
writeOut(toDosTime(ze.getTime()));
written += 4; written += 4;


// CRC // CRC
@@ -669,7 +668,7 @@ public class ZipOutputStream extends FilterOutputStream {
written += 2; written += 2;


// last mod. time and date // last mod. time and date
writeOut(toDosTime(new Date(ze.getTime())));
writeOut(toDosTime(ze.getTime()));
written += 4; written += 4;


// CRC // CRC
@@ -765,12 +764,10 @@ public class ZipOutputStream extends FilterOutputStream {
/** /**
* Convert a Date object to a DOS date/time field. * Convert a Date object to a DOS date/time field.
* *
* <p>Stolen from InfoZip's <code>fileio.c</code></p>
*
* @since 1.1 * @since 1.1
*/ */
protected static ZipLong toDosTime(Date time) { 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 * @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) { if (year < 1980) {
return DOS_TIME_MIN.getBytes(); return DOS_TIME_MIN.getBytes();
} }
int month = cal.get(Calendar.MONTH) + 1;
int month = time.getMonth() + 1;
long value = ((year - 1980) << 25) long value = ((year - 1980) << 25)
| (month << 21) | (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]; byte[] result = new byte[4];
result[0] = (byte) ((value & 0xFF)); result[0] = (byte) ((value & 0xFF));
result[1] = (byte) ((value & 0xFF00) >> 8); result[1] = (byte) ((value & 0xFF00) >> 8);


+ 10
- 8
src/testcases/org/apache/tools/zip/ZipOutputStreamTest.java View File

@@ -21,6 +21,7 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;


import java.util.Calendar;
import java.util.Date; import java.util.Date;


public class ZipOutputStreamTest extends TestCase { public class ZipOutputStreamTest extends TestCase {
@@ -37,22 +38,23 @@ public class ZipOutputStreamTest extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
time = new Date(); 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) long value = ((year - 1980) << 25)
| (month << 21) | (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[0] = (byte) ((value & 0xFF));
result[1] = (byte) ((value & 0xFF00) >> 8); result[1] = (byte) ((value & 0xFF00) >> 8);
result[2] = (byte) ((value & 0xFF0000) >> 16); result[2] = (byte) ((value & 0xFF0000) >> 16);
result[3] = (byte) ((value & 0xFF000000L) >> 24); result[3] = (byte) ((value & 0xFF000000L) >> 24);
zl = new ZipLong(result); zl = new ZipLong(result);
} }


protected void tearDown() throws Exception { protected void tearDown() throws Exception {


Loading…
Cancel
Save