Refactor convertStringToBytes method

This commit is contained in:
Sampo Niskanen 2013-01-06 16:12:12 +02:00
parent e2e509210c
commit 011bcd27e2
3 changed files with 35 additions and 39 deletions

View File

@ -3,7 +3,6 @@ package net.sf.openrocket.file;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
@ -28,9 +27,9 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
private static final int READ_BYTES = 300;
private static final byte[] GZIP_SIGNATURE = { 31, -117 }; // 0x1f, 0x8b
private static final byte[] ZIP_SIGNATURE = TextUtil.convertStringToBytes("PK",Charset.forName("US-ASCII"));
private static final byte[] OPENROCKET_SIGNATURE = TextUtil.convertStringToBytes("<openrocket",Charset.forName("US-ASCII"));
private static final byte[] ROCKSIM_SIGNATURE = TextUtil.convertStringToBytes("<RockSimDoc",Charset.forName("US-ASCII"));
private static final byte[] ZIP_SIGNATURE = TextUtil.asciiBytes("PK");
private static final byte[] OPENROCKET_SIGNATURE = TextUtil.asciiBytes("<openrocket");
private static final byte[] ROCKSIM_SIGNATURE = TextUtil.asciiBytes("<RockSimDoc");
private final OpenRocketLoader openRocketLoader = new OpenRocketLoader();
@ -78,7 +77,7 @@ public class GeneralRocketLoader extends AbstractRocketLoader {
OpenRocketDocument doc = loadFromStream(in, motorFinder);
doc.getDefaultStorageOptions().setCompressionEnabled(true);
return doc;
} else if ( entry.getName().matches(".*\\.[rR][kK][tT]$")) {
} else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) {
OpenRocketDocument doc = loadFromStream(in, motorFinder);
return doc;
}

View File

@ -5,28 +5,24 @@ import java.nio.charset.Charset;
public class TextUtil {
private static final char[] HEX = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
/**
* Return the byte array for the string in the given charset.
* Return the byte array for the string (in US-ASCII charset).
*
* This function is implemented because Froyo (Android API 8) does not support
* String.getBytes(Charset)
*
* @param string
* @param charSet
* @return
*/
public static byte[] convertStringToBytes( String string, Charset charSet ) {
ByteBuffer encoded = charSet.encode(string);
public static byte[] asciiBytes(String string) {
ByteBuffer encoded = Charset.forName("US-ASCII").encode(string);
return encoded.array();
}
/**
* Return the bytes formatted as a hexadecimal string. The length of the
@ -69,7 +65,7 @@ public class TextUtil {
return "Inf";
}
final String sign = (d < 0) ? "-" : "";
double abs = Math.abs(d);
@ -128,8 +124,8 @@ public class TextUtil {
}
/*
* value must be positive!
*/
@ -146,11 +142,11 @@ public class TextUtil {
// Round value
value = (Math.rint(value * rounding) + 0.1) / rounding;
int whole = (int) value;
value -= whole;
if (value < limit)
return "" + whole;
limit *= 10;
@ -159,7 +155,7 @@ public class TextUtil {
sb.append("" + whole);
sb.append('.');
for (int i = 0; i < decimals; i++) {
value *= 10;
@ -188,13 +184,14 @@ public class TextUtil {
/*
* Returns a word-wrapped version of given input string using HTML syntax, wrapped to len characters.
*/
public static String wrap(String in,int len) {
in=in.trim();
if(in.length()<len) return in;
if(in.substring(0, len).contains("\n"))
public static String wrap(String in, int len) {
in = in.trim();
if (in.length() < len)
return in;
if (in.substring(0, len).contains("\n"))
return in.substring(0, in.indexOf("\n")).trim() + "\n\n" + wrap(in.substring(in.indexOf("\n") + 1), len);
int place=Math.max(Math.max(in.lastIndexOf(" ",len),in.lastIndexOf("\t",len)),in.lastIndexOf("-",len));
return "<html>"+in.substring(0,place).trim()+"<br>"+wrap(in.substring(place),len);
int place = Math.max(Math.max(in.lastIndexOf(" ", len), in.lastIndexOf("\t", len)), in.lastIndexOf("-", len));
return "<html>" + in.substring(0, place).trim() + "<br>" + wrap(in.substring(place), len);
}
}

View File

@ -1,8 +1,8 @@
package net.sf.openrocket.util;
import static java.lang.Math.PI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.nio.charset.Charset;
import java.util.Random;
@ -17,19 +17,19 @@ public class TextUtilTest {
Charset us_ascii = Charset.forName("US-ASCII");
byte[] ZIP_SIGNATURE_CORRECT = "PK".getBytes(us_ascii);
byte[] ZIP_SIGNATURE_TEST = TextUtil.convertStringToBytes( "PK", us_ascii);
byte[] ZIP_SIGNATURE_TEST = TextUtil.asciiBytes("PK");
assertArrayEquals( ZIP_SIGNATURE_CORRECT, ZIP_SIGNATURE_TEST );
assertArrayEquals(ZIP_SIGNATURE_CORRECT, ZIP_SIGNATURE_TEST);
byte[] OPENROCKET_SIGNATURE_CORRECT = "<openrocket".getBytes(us_ascii);
byte[] OPENROCKET_SIGNATURE_TEST = TextUtil.convertStringToBytes( "<openrocket", us_ascii);
assertArrayEquals( OPENROCKET_SIGNATURE_CORRECT, OPENROCKET_SIGNATURE_TEST);
byte[] OPENROCKET_SIGNATURE_TEST = TextUtil.asciiBytes("<openrocket");
assertArrayEquals(OPENROCKET_SIGNATURE_CORRECT, OPENROCKET_SIGNATURE_TEST);
byte[] ROCKSIM_SIGNATURE_CORRECT = "<RockSimDoc".getBytes(us_ascii);
byte[] ROCKSIM_SIGNATURE_TEST = TextUtil.convertStringToBytes( "<RockSimDoc", us_ascii);
byte[] ROCKSIM_SIGNATURE_TEST = TextUtil.asciiBytes("<RockSimDoc");
assertArrayEquals( ROCKSIM_SIGNATURE_CORRECT, ROCKSIM_SIGNATURE_TEST );
assertArrayEquals(ROCKSIM_SIGNATURE_CORRECT, ROCKSIM_SIGNATURE_TEST);
}
@Test
@ -187,12 +187,12 @@ public class TextUtilTest {
assertEquals("1.001", TextUtil.doubleToString(1.00096));
/*
* Not testing with 1.00015 because it might be changed during number formatting
* calculations. Its rounding is basically arbitrary anyway.
*/
assertEquals("1.0002e-5", TextUtil.doubleToString(1.0001500001e-5));
assertEquals("1.0001e-5", TextUtil.doubleToString(1.0001499999e-5));
assertEquals("1.0002e-4", TextUtil.doubleToString(1.0001500001e-4));
@ -226,7 +226,7 @@ public class TextUtilTest {
assertEquals("1.0002e10", TextUtil.doubleToString(1.0001500001e10));
assertEquals("1.0001e10", TextUtil.doubleToString(1.0001499999e10));
assertEquals("-1.0002e-5", TextUtil.doubleToString(-1.0001500001e-5));
assertEquals("-1.0001e-5", TextUtil.doubleToString(-1.0001499999e-5));
assertEquals("-1.0002e-4", TextUtil.doubleToString(-1.0001500001e-4));