2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
2013-05-28 19:29:24 +00:00
|
|
|
* Copyright (c) 2013 Ziver
|
|
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
2013-05-28 19:29:24 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
package zutil;
|
|
|
|
|
|
2014-04-01 19:59:35 +00:00
|
|
|
import zutil.converters.Converter;
|
|
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* This is a class whit utility methods.
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver *
|
|
|
|
|
*/
|
|
|
|
|
public class StringUtil {
|
2010-08-18 17:35:25 +00:00
|
|
|
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
2010-10-27 13:49:46 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* Present a size (in bytes) as a human-readable value
|
|
|
|
|
*
|
2014-04-01 19:59:35 +00:00
|
|
|
* @param bytes size (in bytes)
|
2008-11-14 16:38:36 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-04-01 19:59:35 +00:00
|
|
|
public static String formatByteSizeToString(long bytes){
|
2008-11-14 16:38:36 +00:00
|
|
|
int total = sizes.length-1;
|
|
|
|
|
double value = bytes;
|
|
|
|
|
|
|
|
|
|
for(; value > 1024 ;total--) {
|
|
|
|
|
value /= 1024;
|
|
|
|
|
}
|
2015-04-19 21:06:01 +00:00
|
|
|
|
2010-08-18 17:35:25 +00:00
|
|
|
value = (double)( (int)(value*10) )/10;
|
2008-11-14 16:38:36 +00:00
|
|
|
return value+" "+sizes[total];
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-15 19:18:18 +00:00
|
|
|
public static String formatTimeToString(long milisec){
|
|
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
|
long tmp = 0;
|
|
|
|
|
|
|
|
|
|
// Years
|
|
|
|
|
if( milisec >= 31557032762.3361d ){
|
|
|
|
|
tmp = (long) (milisec / 31557032762.3361d);
|
|
|
|
|
milisec -= tmp * 31557032762.3361d;
|
|
|
|
|
if( tmp > 1 )
|
|
|
|
|
str.append(tmp).append(" years ");
|
|
|
|
|
else
|
|
|
|
|
str.append(tmp).append(" year ");
|
|
|
|
|
}
|
|
|
|
|
// Months
|
|
|
|
|
if( milisec >= 2629743830l ){
|
|
|
|
|
tmp = (long) (milisec / 2629743830l);
|
|
|
|
|
milisec -= tmp * 2629743830l;
|
|
|
|
|
if( tmp > 1 )
|
|
|
|
|
str.append(tmp).append(" months ");
|
|
|
|
|
else
|
|
|
|
|
str.append(tmp).append(" month ");
|
|
|
|
|
}
|
|
|
|
|
// Days
|
|
|
|
|
if( milisec >= 86400000 ){
|
|
|
|
|
tmp = (long) (milisec / 86400000);
|
|
|
|
|
milisec -= tmp * 86400000;
|
|
|
|
|
if( tmp > 1 )
|
|
|
|
|
str.append(tmp).append(" days ");
|
|
|
|
|
else
|
|
|
|
|
str.append(tmp).append(" day ");
|
|
|
|
|
}
|
|
|
|
|
// Hours
|
|
|
|
|
if( milisec >= 3600000 ){
|
|
|
|
|
tmp = (long) (milisec / 3600000);
|
|
|
|
|
milisec -= tmp * 3600000;
|
|
|
|
|
if( tmp > 1 )
|
|
|
|
|
str.append(tmp).append(" hours ");
|
|
|
|
|
else
|
|
|
|
|
str.append(tmp).append(" hour ");
|
|
|
|
|
}
|
|
|
|
|
// Minutes
|
|
|
|
|
if( milisec >= 60000 ){
|
|
|
|
|
tmp = (long) (milisec / 60000);
|
|
|
|
|
milisec -= tmp * 60000;
|
|
|
|
|
str.append(tmp).append(" min ");
|
|
|
|
|
}
|
|
|
|
|
// sec
|
|
|
|
|
if( milisec >= 1000 ){
|
|
|
|
|
tmp = (long) (milisec / 1000);
|
|
|
|
|
milisec -= tmp * 1000;
|
|
|
|
|
str.append(tmp).append(" sec ");
|
|
|
|
|
}
|
|
|
|
|
if( milisec > 0 ){
|
|
|
|
|
str.append(milisec).append(" milisec ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 13:49:46 +00:00
|
|
|
/**
|
|
|
|
|
* Trims the given char and whitespace at the beginning and the end
|
|
|
|
|
*
|
|
|
|
|
* @param str is the string to trim
|
|
|
|
|
* @param trim is the char to trim
|
|
|
|
|
* @return a trimmed String
|
|
|
|
|
*/
|
|
|
|
|
public static String trim(String str, char trim){
|
|
|
|
|
if( str == null || str.isEmpty() )
|
|
|
|
|
return str;
|
|
|
|
|
int start=0, stop=str.length();
|
|
|
|
|
// The beginning
|
|
|
|
|
for(int i=0; i<str.length() ;i++){
|
|
|
|
|
char c = str.charAt( i );
|
|
|
|
|
if( c <= ' ' || c == trim )
|
|
|
|
|
start = i+1;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// The end
|
|
|
|
|
for(int i=str.length()-1; i>start ;i--){
|
|
|
|
|
char c = str.charAt( i );
|
|
|
|
|
if( c <= ' ' || c == trim )
|
|
|
|
|
stop = i;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if( start >= str.length() )
|
|
|
|
|
return "";
|
|
|
|
|
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
|
|
|
|
return str.substring(start, stop);
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
|
2010-10-27 13:49:46 +00:00
|
|
|
/**
|
|
|
|
|
* Trims the whitespace and quotes if the string starts and ends with one
|
|
|
|
|
*
|
2014-04-01 19:59:35 +00:00
|
|
|
* @param str is the string to trim
|
2010-10-27 13:49:46 +00:00
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String trimQuotes(String str){
|
|
|
|
|
if( str == null )
|
|
|
|
|
return null;
|
|
|
|
|
str = str.trim();
|
|
|
|
|
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
|
|
|
|
str = str.substring(1, str.length()-1);
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2014-04-01 19:59:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Presents a binary array in HEX and ASCII
|
|
|
|
|
*
|
|
|
|
|
* @param data The source binary data to format
|
|
|
|
|
* @return A multiline String with human readable HEX and ASCII
|
|
|
|
|
*/
|
|
|
|
|
public static String formatBytesToString(byte[] data){
|
|
|
|
|
StringBuffer output = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
//000 XX XX XX XX XX XX XX XX '........'
|
|
|
|
|
int maxOffset = (""+data.length).length();
|
|
|
|
|
for(int offset=0; offset<data.length; offset+=8){
|
|
|
|
|
if(offset != 0)
|
|
|
|
|
output.append('\n');
|
|
|
|
|
|
|
|
|
|
// Offset
|
|
|
|
|
String offsetStr = ""+offset;
|
|
|
|
|
for(int i=offsetStr.length(); i<3 || i<maxOffset; ++i){
|
|
|
|
|
output.append('0');
|
|
|
|
|
}
|
|
|
|
|
output.append(offsetStr);
|
|
|
|
|
output.append(" ");
|
|
|
|
|
|
|
|
|
|
// HEX
|
|
|
|
|
for(int i=0; i<8; ++i){
|
|
|
|
|
if(offset+i < data.length)
|
|
|
|
|
output.append(Converter.toHexString(data[offset+i]));
|
|
|
|
|
else
|
|
|
|
|
output.append(" ");
|
|
|
|
|
output.append(' ');
|
|
|
|
|
}
|
|
|
|
|
output.append(' ');
|
|
|
|
|
|
|
|
|
|
// ACII
|
|
|
|
|
output.append('\'');
|
|
|
|
|
for(int i=0; i<8; ++i){
|
|
|
|
|
if(offset+i < data.length)
|
|
|
|
|
if( 32 <= data[offset+i] && data[offset+i] <= 126 )
|
|
|
|
|
output.append((char)data[offset+i]);
|
|
|
|
|
else
|
|
|
|
|
output.append('.');
|
|
|
|
|
else
|
|
|
|
|
output.append(' ');
|
|
|
|
|
}
|
|
|
|
|
output.append('\'');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output.toString();
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|