Browse Source

remove some magic numbers

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@568704 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 18 years ago
parent
commit
ddd5fc4276
4 changed files with 66 additions and 37 deletions
  1. +34
    -16
      src/main/org/apache/tools/ant/types/selectors/SizeSelector.java
  2. +3
    -1
      src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java
  3. +3
    -1
      src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java
  4. +26
    -19
      src/main/org/apache/tools/zip/ZipOutputStream.java

+ 34
- 16
src/main/org/apache/tools/ant/types/selectors/SizeSelector.java View File

@@ -31,6 +31,24 @@ import org.apache.tools.ant.types.Parameter;
*/ */
public class SizeSelector extends BaseExtendSelector { public class SizeSelector extends BaseExtendSelector {


/** Constants for kilo, kibi etc */
private static final int KILO = 1000;
private static final int KIBI = 1024;
private static final int KIBI_POS = 4;
private static final int MEGA = 1000000;
private static final int MEGA_POS = 9;
private static final int MEBI = 1048576;
private static final int MEBI_POS = 13;
private static final long GIGA = 1000000000L;
private static final int GIGA_POS = 18;
private static final long GIBI = 1073741824L;
private static final int GIBI_POS = 22;
private static final long TERA = 1000000000000L;
private static final int TERA_POS = 27;
private static final long TEBI = 1099511627776L;
private static final int TEBI_POS = 31;
private static final int END_POS = 36;

/** Used for parameterized custom selector */ /** Used for parameterized custom selector */
public static final String SIZE_KEY = "value"; public static final String SIZE_KEY = "value";
/** Used for parameterized custom selector */ /** Used for parameterized custom selector */
@@ -107,22 +125,22 @@ public class SizeSelector extends BaseExtendSelector {
public void setUnits(ByteUnits units) { public void setUnits(ByteUnits units) {
int i = units.getIndex(); int i = units.getIndex();
multiplier = 0; multiplier = 0;
if (i > -1 && i < 4) {
multiplier = 1000;
} else if (i > 3 && i < 9) {
multiplier = 1024;
} else if (i > 8 && i < 13) {
multiplier = 1000000;
} else if (i > 12 && i < 18) {
multiplier = 1048576;
} else if (i > 17 && i < 22) {
multiplier = 1000000000L;
} else if (i > 21 && i < 27) {
multiplier = 1073741824L;
} else if (i > 26 && i < 31) {
multiplier = 1000000000000L;
} else if (i > 30 && i < 36) {
multiplier = 1099511627776L;
if (i > -1 && i < KIBI_POS) {
multiplier = KILO;
} else if (i < MEGA_POS) {
multiplier = KIBI;
} else if (i < MEBI_POS) {
multiplier = MEGA;
} else if (i < GIGA_POS) {
multiplier = MEBI;
} else if (i < GIBI_POS) {
multiplier = GIGA;
} else if (i < TERA_POS) {
multiplier = GIBI;
} else if (i < TEBI_POS) {
multiplier = TERA;
} else if (i < END_POS) {
multiplier = TEBI;
} }
if (multiplier > 0 && size > -1) { if (multiplier > 0 && size > -1) {
sizelimit = size * multiplier; sizelimit = size * multiplier;


+ 3
- 1
src/main/org/apache/tools/ant/util/regexp/RegexpFactory.java View File

@@ -71,7 +71,9 @@ public class RegexpFactory extends RegexpMatcherFactory {
testAvailability("java.util.regex.Matcher"); testAvailability("java.util.regex.Matcher");
return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp"); return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp");
} catch (BuildException be) { } catch (BuildException be) {
cause = orCause(cause, be, JavaEnvUtils.getJavaVersionNumber() < 14);
cause = orCause(
cause, be,
JavaEnvUtils.getJavaVersionNumber() < JavaEnvUtils.VERSION_1_4);
} }


try { try {


+ 3
- 1
src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java View File

@@ -74,7 +74,9 @@ public class RegexpMatcherFactory {
testAvailability("java.util.regex.Matcher"); testAvailability("java.util.regex.Matcher");
return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher"); return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher");
} catch (BuildException be) { } catch (BuildException be) {
cause = orCause(cause, be, JavaEnvUtils.getJavaVersionNumber() < 14);
cause = orCause(
cause, be,
JavaEnvUtils.getJavaVersionNumber() < JavaEnvUtils.VERSION_1_4);
} }


try { try {


+ 26
- 19
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -54,6 +54,10 @@ import java.util.zip.ZipException;
*/ */
public class ZipOutputStream extends FilterOutputStream { public class ZipOutputStream extends FilterOutputStream {


private static final int BYTE_MASK = 0xFF;
private static final int SHORT = 2;
private static final int WORD = 4;

/** /**
* Compression method for deflated entries. * Compression method for deflated entries.
* *
@@ -500,7 +504,7 @@ public class ZipOutputStream extends FilterOutputStream {
*/ */
public void write(int b) throws IOException { public void write(int b) throws IOException {
byte[] buff = new byte[1]; byte[] buff = new byte[1];
buff[0] = (byte) (b & 0xff);
buff[0] = (byte) (b & BYTE_MASK);
write(buff, 0, 1); write(buff, 0, 1);
} }


@@ -587,7 +591,7 @@ public class ZipOutputStream extends FilterOutputStream {
offsets.put(ze, ZipLong.getBytes(written)); offsets.put(ze, ZipLong.getBytes(written));


writeOut(LFH_SIG); writeOut(LFH_SIG);
written += 4;
written += WORD;


//store method in local variable to prevent multiple method calls //store method in local variable to prevent multiple method calls
final int zipMethod = ze.getMethod(); final int zipMethod = ze.getMethod();
@@ -605,15 +609,15 @@ public class ZipOutputStream extends FilterOutputStream {
writeOut(ZipShort.getBytes(10)); writeOut(ZipShort.getBytes(10));
writeOut(ZERO); writeOut(ZERO);
} }
written += 4;
written += WORD;


// compression method // compression method
writeOut(ZipShort.getBytes(zipMethod)); writeOut(ZipShort.getBytes(zipMethod));
written += 2;
written += SHORT;


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


// CRC // CRC
// compressed length // compressed length
@@ -633,12 +637,12 @@ public class ZipOutputStream extends FilterOutputStream {
// file name length // file name length
byte[] name = getBytes(ze.getName()); byte[] name = getBytes(ze.getName());
writeOut(ZipShort.getBytes(name.length)); writeOut(ZipShort.getBytes(name.length));
written += 2;
written += SHORT;


// extra field length // extra field length
byte[] extra = ze.getLocalFileDataExtra(); byte[] extra = ze.getLocalFileDataExtra();
writeOut(ZipShort.getBytes(extra.length)); writeOut(ZipShort.getBytes(extra.length));
written += 2;
written += SHORT;


// file name // file name
writeOut(name); writeOut(name);
@@ -678,11 +682,11 @@ public class ZipOutputStream extends FilterOutputStream {
*/ */
protected void writeCentralFileHeader(ZipEntry ze) throws IOException { protected void writeCentralFileHeader(ZipEntry ze) throws IOException {
writeOut(CFH_SIG); writeOut(CFH_SIG);
written += 4;
written += WORD;


// version made by // version made by
writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20)); writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20));
written += 2;
written += SHORT;


// version needed to extract // version needed to extract
// general purpose bit flag // general purpose bit flag
@@ -697,15 +701,15 @@ public class ZipOutputStream extends FilterOutputStream {
writeOut(ZipShort.getBytes(10)); writeOut(ZipShort.getBytes(10));
writeOut(ZERO); writeOut(ZERO);
} }
written += 4;
written += WORD;


// compression method // compression method
writeOut(ZipShort.getBytes(ze.getMethod())); writeOut(ZipShort.getBytes(ze.getMethod()));
written += 2;
written += SHORT;


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


// CRC // CRC
// compressed length // compressed length
@@ -718,12 +722,12 @@ public class ZipOutputStream extends FilterOutputStream {
// file name length // file name length
byte[] name = getBytes(ze.getName()); byte[] name = getBytes(ze.getName());
writeOut(ZipShort.getBytes(name.length)); writeOut(ZipShort.getBytes(name.length));
written += 2;
written += SHORT;


// extra field length // extra field length
byte[] extra = ze.getCentralDirectoryExtra(); byte[] extra = ze.getCentralDirectoryExtra();
writeOut(ZipShort.getBytes(extra.length)); writeOut(ZipShort.getBytes(extra.length));
written += 2;
written += SHORT;


// file comment length // file comment length
String comm = ze.getComment(); String comm = ze.getComment();
@@ -732,23 +736,23 @@ public class ZipOutputStream extends FilterOutputStream {
} }
byte[] commentB = getBytes(comm); byte[] commentB = getBytes(comm);
writeOut(ZipShort.getBytes(commentB.length)); writeOut(ZipShort.getBytes(commentB.length));
written += 2;
written += SHORT;


// disk number start // disk number start
writeOut(ZERO); writeOut(ZERO);
written += 2;
written += SHORT;


// internal file attributes // internal file attributes
writeOut(ZipShort.getBytes(ze.getInternalAttributes())); writeOut(ZipShort.getBytes(ze.getInternalAttributes()));
written += 2;
written += SHORT;


// external file attributes // external file attributes
writeOut(ZipLong.getBytes(ze.getExternalAttributes())); writeOut(ZipLong.getBytes(ze.getExternalAttributes()));
written += 4;
written += WORD;


// relative offset of LFH // relative offset of LFH
writeOut((byte[]) offsets.get(ze)); writeOut((byte[]) offsets.get(ze));
written += 4;
written += WORD;


// file name // file name
writeOut(name); writeOut(name);
@@ -818,6 +822,8 @@ public class ZipOutputStream extends FilterOutputStream {
*/ */
protected static byte[] toDosTime(long t) { protected static byte[] toDosTime(long t) {
Date time = new Date(t); Date time = new Date(t);
// CheckStyle:MagicNumberCheck OFF - I do not think that using constants
// here will improve the readablity
int year = time.getYear() + 1900; int year = time.getYear() + 1900;
if (year < 1980) { if (year < 1980) {
return DOS_TIME_MIN; return DOS_TIME_MIN;
@@ -830,6 +836,7 @@ public class ZipOutputStream extends FilterOutputStream {
| (time.getMinutes() << 5) | (time.getMinutes() << 5)
| (time.getSeconds() >> 1); | (time.getSeconds() >> 1);
return ZipLong.getBytes(value); return ZipLong.getBytes(value);
// CheckStyle:MagicNumberCheck ON
} }


/** /**


Loading…
Cancel
Save