From: megacz Date: Fri, 30 Jan 2004 07:41:22 +0000 (+0000) Subject: 2003/11/14 07:47:05 X-Git-Tag: RC3~351 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=582fe3be8305d085f0f2c98eb8cbd32558cc6296;p=org.ibex.core.git 2003/11/14 07:47:05 darcs-hash:20040130074122-2ba56-f8269657aaebf8e89190fa39cf54621b80270220.gz --- diff --git a/src/org/xwt/util/PackBytesIntoString.java b/src/org/xwt/util/PackBytesIntoString.java new file mode 100644 index 0000000..6579f7e --- /dev/null +++ b/src/org/xwt/util/PackBytesIntoString.java @@ -0,0 +1,42 @@ +package org.xwt.util; +import java.io.*; +import java.util.*; + +/** packs 8-bit bytes into a String of 7-bit chars (to avoid the UTF-8 non-ASCII penalty) */ +public class PackBytesIntoString { + + public static String pack(byte[] b, int off, int len) throws IllegalArgumentException { + if (len % 7 != 0) throw new IllegalArgumentException("len must be a multiple of 7"); + StringBuffer ret = new StringBuffer(); + for(int i=off; i=0; j--) { + l <<= 8; + l |= (b[i + j] & 0xff); + } + for(int j=0; j<8; j++) { + ret.append((char)(l & 0x7f)); + l >>= 7; + } + } + return ret.toString(); + } + + public static byte[] unpack(String s) throws IllegalArgumentException { + if (s.length() % 8 != 0) throw new IllegalArgumentException("string length must be a multiple of 8"); + byte[] ret = new byte[(s.length() / 8) * 7]; + for(int i=0; i=0; j--) { + l <<= 7; + l |= (s.charAt(i + j) & 0x7fL); + } + for(int j=0; j<7; j++) { + ret[(i / 8) * 7 + j] = (byte)(l & 0xff); + l >>= 8; + } + } + return ret; + } +} +