public static String trimSize(String str,int size){
  if (str == null)   return str;
  if (size <= 0)   throw new IllegalArgumentException("Trim size invalid: " + size);
  int length=str.length();
  if (length < size / 2)   return str;
  try {
    byte[] bytes=str.getBytes("UTF-8");
    if (bytes.length <= size)     return str;
    byte[] result=new byte[size];
    System.arraycopy(bytes,0,result,0,size);
    return new String(result,"UTF-8");
  }
 catch (  UnsupportedEncodingException e) {
    log.log(Level.SEVERE,str,e);
  }
  return str;
}

public static String trimSize ( String str , int size ) { if ( str == null ) return str ; if ( size <= 0 ) throw new IllegalArgumentException ( "Trim size invalid: " + size ) ; int length = str . length ( ) ; if ( length < size / 2 ) return str ; try { byte [ ] bytes = str . getBytes ( "UTF-8" ) ; if ( bytes . length <= size ) return str ; byte [ ] result = new byte [ size ] ; System . arraycopy ( bytes , 0 , result , 0 , size ) ; return new String ( result , "UTF-8" ) ; } catch ( UnsupportedEncodingException e ) { log . log ( Level . SEVERE , str , e ) ; } return str ; }
trim to max byte size
