import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class StringUtil {
public static String[] toUpper(String[] inStr)
{
boolean bEmpty=true;
for (int i=0; i
if (inStr[i]!=null && !inStr[i].equals("") && bEmpty) bEmpty=false;
inStr[i] = inStr[i] != null ? inStr[i].toUpperCase():"";
}
if (bEmpty) return null;
else return inStr;
}
public static int getStringByteLength(String data) {
byte[] utf8 = null;
int byteCount = 0;
try {
utf8 = data.getBytes("UTF-8");
byteCount = utf8.length;
} catch (Exception ex) {
ex.printStackTrace();
}
return byteCount;
}
public static String[] parseCSV(String lineRead) throws Exception {
String data = "";
String tempLine = "";
boolean proceed = true;
boolean isQuotes = false;
int index = 0;
String tokens[] = new String[1000];
String tokenReturn[] ;
lineRead=lineRead+',';
for (int i = 0; i < lineRead.length() - 1; i++) {
data += lineRead.charAt(i);
if (lineRead.charAt(i) == '"' || lineRead.charAt(i) == '^') {
proceed = false;
isQuotes = true;
}
if (proceed == false && (lineRead.charAt(i) == '"' || lineRead.charAt(i) == '^')
&& lineRead.charAt(i + 1) == ',')
proceed = true;
if ((proceed == true && lineRead.charAt(i) == ',')) {
tempLine = tempLine + data;
if (isQuotes){
if(data.trim().length() > 2) {
tokens[index] = data.trim().substring(1, data.trim().length() -2);
}
else
tokens[index] = data.trim();
}
else{
if(data!=null)
tokens[index] = data.trim().substring(0, data.trim().length() - 1);
else
tokens[index]="";
}
data = "";
index++;
isQuotes = false;
}
}
if (tempLine.length() != lineRead.length()) {
if( (data.trim().startsWith("\"") || data.trim().startsWith("^")) && data.length() >= 2)
tokens[index] = data.trim().substring(1, data.trim().length() -1);
else
tokens[index] = data.trim();
index++;
}
tokenReturn = new String[index];
System.arraycopy( tokens, 0, tokenReturn, 0, index);
return tokenReturn;
}
/**
* Convert a given byte[] with charset name into UTF8 format.
*/
public static String toUTF8(String charsetname, byte[] bytes) throws Exception
{
//logger.debug("encoding input:" + new String(bytes, charsetname) + ",length:" + bytes.length);
Charset charset = Charset.forName(charsetname);
ByteBuffer bb = ByteBuffer.wrap(bytes);
CharBuffer cb = charset.decode(bb);
//logger.debug("Length:" + cb.length() + ",UTF-8 output:" + cb.toString());
return cb.toString();
}
/**
* Reverse of toUTF8. Converts UTF8 to individual charset.
*/
public static byte[] fromUTF8(String charsetname, String str) throws Exception
{
//logger.debug("encoding input:" + new String(bytes, charsetname) + ",length:" + bytes.length);
Charset charset = Charset.forName(charsetname);
ByteBuffer bb = charset.encode(str);
byte[] dst = new byte[bb.capacity()];
bb.position(0);
bb.get(dst, 0, bb.capacity());
return dst;
//return new String(dst, charsetname);
//return bb.toString();
}
public static String getErrorStackString(Exception e){
StackTraceElement[] se = e.getStackTrace();
String strse="";
for (int i=0; i
strse += se[i].toString() + "\n";
}
return strse;
}
}

No comments:
Post a Comment