From 011bcd27e2110a62a8893b2120ddc96a8136602a Mon Sep 17 00:00:00 2001 From: Sampo Niskanen Date: Sun, 6 Jan 2013 16:12:12 +0200 Subject: [PATCH] Refactor convertStringToBytes method --- .../openrocket/file/GeneralRocketLoader.java | 9 ++-- core/src/net/sf/openrocket/util/TextUtil.java | 43 +++++++++---------- .../net/sf/openrocket/util/TextUtilTest.java | 22 +++++----- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index da6858d89..f6ef111f1 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -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(""+in.substring(0,place).trim()+"
"+wrap(in.substring(place),len); + int place = Math.max(Math.max(in.lastIndexOf(" ", len), in.lastIndexOf("\t", len)), in.lastIndexOf("-", len)); + return "" + in.substring(0, place).trim() + "
" + wrap(in.substring(place), len); } - + } diff --git a/core/test/net/sf/openrocket/util/TextUtilTest.java b/core/test/net/sf/openrocket/util/TextUtilTest.java index 4c040fbbe..a408b3c38 100644 --- a/core/test/net/sf/openrocket/util/TextUtilTest.java +++ b/core/test/net/sf/openrocket/util/TextUtilTest.java @@ -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 = "