/* infoScoop OpenSource
* Copyright (C) 2010 Beacon IT Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* .
*/
/**
* A utility class related to character string.
*
* @author Eiichi Sakurai
*/
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
public class Util{
/**
* @param originalString Original character string
* @param byteLength A necessary byte length
* @param charset Because we use DB2, we fix "UTF-8".
* @return
* @throws UnsupportedEncodingException
*/
public static String getTruncatedString(String originalString, int byteLength, String charset) throws UnsupportedEncodingException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] originalBytes = originalString.getBytes(charset);
if(originalBytes.length <= byteLength) return originalString;
out.write(originalBytes, 0 , byteLength + 1);
String result = new String(out.toByteArray(), charset);
return result.substring(0, result.length()-1);
}
}