Last active
August 29, 2015 14:01
-
-
Save pdemanget/ba4988e9cdeb5220cbf1 to your computer and use it in GitHub Desktop.
reduce byte length of a String (and its useless optimization)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringReduceTest { | |
private static final Charset UTF8 = Charset.forName("UTF-8"); | |
public String reduceStringUTF8Size(String s, int size) { | |
while(s.getBytes(UTF8).length >size){ | |
s=s.substring(0,s.length()-1); | |
} | |
return s; | |
} | |
public String reduceStringUTF8Size2(String s, int size) throws CharacterCodingException { | |
int newsize=0; | |
int length = s.length(); | |
CharsetEncoder ce = UTF8.newEncoder(); | |
int pos = 0; | |
for(int i=0; i<length;i++){ | |
char[] c= {s.charAt(i)}; | |
newsize += ce.encode(CharBuffer.wrap(c)).limit(); | |
if(newsize>size) { | |
break; | |
} | |
pos = i; | |
} | |
return s.substring(0, pos + 1); | |
} | |
@Test | |
public void monTest() throws UnsupportedEncodingException, CharacterCodingException{ | |
String s10 = "éèç@à$¤%ùµ"; | |
// String s10 = "éèç@à$¤%ùµ"; | |
Assert.assertEquals(s10.length(),10); | |
String s200=s10+s10+s10+s10+s10+s10+s10+s10+s10+s10; | |
s200 += s200; | |
Assert.assertEquals(s200.length(),200); | |
System.out.println(s200.getBytes("UTF-8").length); | |
System.out.println(s200.getBytes("ISO8859_1").length); | |
String s200bytes = null; | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 1000; i++) { | |
s200bytes=reduceStringUTF8Size(s200,200); | |
} | |
long v1 = System.currentTimeMillis() - start; | |
System.out.println(s200bytes.getBytes("UTF-8").length); | |
System.out.println(s200bytes); | |
System.out.println(s200bytes.length()); | |
Assert.assertEquals(s200bytes.getBytes("UTF-8").length,200); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < 1000; i++) { | |
s200bytes=reduceStringUTF8Size2(s200,200); | |
} | |
long v2 = System.currentTimeMillis() - start; | |
System.out.println(s200bytes.getBytes("UTF-8").length); | |
System.out.println(s200bytes); | |
System.out.println(s200bytes.length()); | |
Assert.assertEquals(s200bytes.getBytes("UTF-8").length,200); | |
System.out.println("v1 = " + v1 + ", v2 = " + v2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment