소스정리
parent
346e19a251
commit
6eaf39691f
@ -1,295 +0,0 @@
|
||||
package util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
/**
|
||||
* 문자열 처리 공통 클래스<BR>
|
||||
* @since JDK 1.4.1
|
||||
* @version 0.1, 2006-12-20 초기 작성
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
private static final char[] toHex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
public static String toHexString(byte b[]) {
|
||||
return toHexString(b, 0, b.length);
|
||||
}
|
||||
|
||||
public static String toHexString(byte b[], int spos, int epos) {
|
||||
int pos = 0;
|
||||
char[] cbuf = new char[(epos - spos) * 2];
|
||||
|
||||
for (int i = spos; i < epos; i++) {
|
||||
cbuf[pos++] = toHex[(b[i] >> 4) & 0x0F];
|
||||
cbuf[pos++] = toHex[b[i] & 0x0F];
|
||||
}
|
||||
return new String(cbuf);
|
||||
}
|
||||
|
||||
public static byte [] toHexBytes(String str) {
|
||||
String recv_data = str.toUpperCase();
|
||||
byte [] aBuff = null;
|
||||
if( recv_data.length()%2 == 0){
|
||||
aBuff = new byte[recv_data.length()];
|
||||
}else{
|
||||
aBuff = new byte[recv_data.length()+1];
|
||||
aBuff[recv_data.length()] = 0x00;
|
||||
}
|
||||
|
||||
for(int i=0; i< recv_data.length(); i++){
|
||||
char buff = recv_data.charAt(i);
|
||||
if( buff >= '0' && buff <= '9'){
|
||||
aBuff[i] = (byte)(buff-48);
|
||||
}else{
|
||||
aBuff[i] = (byte)(buff-55);
|
||||
}
|
||||
}
|
||||
byte [] bBuff = new byte[aBuff.length/2];
|
||||
|
||||
for(int i=0; i< aBuff.length/2; i++){
|
||||
byte a = (byte)( aBuff[i*2+0] << 4 & 0xF0);
|
||||
byte b = (byte)( aBuff[i*2+1] & 0x0F);
|
||||
bBuff[i] = (byte)( a | b);
|
||||
}
|
||||
return bBuff;
|
||||
}
|
||||
|
||||
public static long toLong(byte b[]) {
|
||||
return toLong(b, 0, b.length);
|
||||
}
|
||||
|
||||
public static long toLong(byte b[], int spos, int epos) {
|
||||
long num = 0;
|
||||
int pos = 0;
|
||||
|
||||
for (int i=epos - 1; i>= spos; i--, pos++) {
|
||||
num |= (b[i] & 0xFF) << (8 * pos);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static boolean isSupportedEncoding(String charset) {
|
||||
try {
|
||||
byte[] b = charset.getBytes("8859_1");
|
||||
String value = new String(b, charset);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String toEncString(String str, String charset) {
|
||||
try {
|
||||
byte[] b = str.getBytes("8859_1");
|
||||
return new String(b, charset);
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toWhiteSpace(String str) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
String whites = " \t\r\n";
|
||||
boolean before = false;
|
||||
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
int p = whites.indexOf(c);
|
||||
|
||||
if (before && p >= 0)
|
||||
continue;
|
||||
|
||||
if (p >= 0) {
|
||||
buffer.append(" ");
|
||||
before = true;
|
||||
}
|
||||
else {
|
||||
buffer.append(c);
|
||||
before = false;
|
||||
}
|
||||
}
|
||||
return new String(buffer.toString().trim());
|
||||
}
|
||||
|
||||
public static String toEncString(byte b[], String charset) {
|
||||
try {
|
||||
return new String(b, charset);
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String MD5(String str) {
|
||||
return MD5(str.getBytes());
|
||||
}
|
||||
|
||||
public static String MD5(byte[] msg) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(msg);
|
||||
|
||||
byte[] digest = md.digest();
|
||||
StringBuffer buf = new StringBuffer(digest.length * 2);
|
||||
|
||||
for (int i = 0; i < digest.length; i++) {
|
||||
int intVal = digest[i] & 0xff;
|
||||
|
||||
if (intVal < 0x10) {
|
||||
buf.append("0");
|
||||
}
|
||||
|
||||
buf.append(Integer.toHexString(intVal));
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
catch (NoSuchAlgorithmException ne) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String ntos(int num, int len) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append(Integer.toString(num));
|
||||
|
||||
while (buffer.length() < len) {
|
||||
buffer.insert(0, "0");
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static String ltos(long num, int len) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append(Long.toString(num));
|
||||
|
||||
while (buffer.length() < len) {
|
||||
buffer.insert(0, "0");
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static String getString(char c, int length) {
|
||||
char[] chars = new char[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
chars[i] = c;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public static String getDate(String format) {
|
||||
return new SimpleDateFormat(format, Locale.KOREA).format(new Date());
|
||||
}
|
||||
|
||||
public static String getTomorrow(String format){
|
||||
Date today = new Date();
|
||||
Date tomorrow = new Date();
|
||||
tomorrow.setTime( today.getTime() + (long) ( 1000 * 60 * 60 * 24 ) * 1 );
|
||||
return new SimpleDateFormat(format, Locale.KOREA).format(tomorrow);
|
||||
}
|
||||
|
||||
public static final boolean LEFT = true;
|
||||
public static final boolean RIGHT = false;
|
||||
|
||||
public static String justify(
|
||||
boolean direction, String src, String filler, int length) {
|
||||
if (src == null || filler == null || length < 0) {
|
||||
return "";
|
||||
}
|
||||
int len = src.getBytes().length;
|
||||
if (len > length) {
|
||||
return src.substring(0, length);
|
||||
}
|
||||
if (len == length) {
|
||||
return src;
|
||||
}
|
||||
StringBuffer buf = direction == LEFT ? new StringBuffer(src) : new StringBuffer();
|
||||
int fillLength = length - len;
|
||||
for (int i = 0; i < fillLength; i++) {
|
||||
buf.append(filler);
|
||||
}
|
||||
if (direction == RIGHT) {
|
||||
buf.append(src);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
/**
|
||||
* 특정구분자로 문자열을 자르는 메소드
|
||||
* @param src
|
||||
* @param filler
|
||||
* @param nArrSize
|
||||
* @return String []
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String [] split( String src, byte filler, int nArrSize) throws Exception {
|
||||
int iArrIndex = 0;
|
||||
int iStartIndex = 0;
|
||||
String [] saReturn = new String[100];
|
||||
byte[] byteOrigin = src.getBytes();
|
||||
byte[] byteArrTemp = null;
|
||||
for(int idx=0; idx<byteOrigin.length;idx++){
|
||||
if(byteOrigin[idx]==filler){
|
||||
if(iStartIndex==idx) saReturn[iArrIndex] = "";
|
||||
else if(iStartIndex==idx-1) {
|
||||
char bTemp = (char)byteOrigin[iStartIndex];
|
||||
saReturn[iArrIndex] = String.valueOf(bTemp);
|
||||
}
|
||||
else {
|
||||
byteArrTemp = getByteArray(byteOrigin, iStartIndex, idx);
|
||||
saReturn[iArrIndex] = new String(byteArrTemp);
|
||||
}
|
||||
iArrIndex++;
|
||||
iStartIndex=idx+1;
|
||||
}
|
||||
}
|
||||
if(iStartIndex==byteOrigin.length) saReturn[iArrIndex] = "";
|
||||
else if(iStartIndex==byteOrigin.length-1) {
|
||||
char bTemp = (char)byteOrigin[iStartIndex];
|
||||
saReturn[iArrIndex] = String.valueOf(bTemp);
|
||||
}
|
||||
else {
|
||||
byteArrTemp = getByteArray(byteOrigin, iStartIndex, byteOrigin.length);
|
||||
saReturn[iArrIndex] = new String(byteArrTemp);
|
||||
}
|
||||
return saReturn;
|
||||
}
|
||||
/**
|
||||
* byte 배열의 일부분을 Return 해주는 메소드
|
||||
* @param bByteData
|
||||
* @param iStart
|
||||
* @param iEnd
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] getByteArray(byte[] bByteData, int iStart, int iEnd)
|
||||
{
|
||||
byte[] b = new byte[iEnd-iStart];
|
||||
for( int i=iStart; i< iEnd; i++) {
|
||||
int c = (bByteData[i] & 0xff) ;
|
||||
b[i-iStart] = (byte) c;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
/*
|
||||
* 라인 문자열에는 한글이 들어가므로 정확한 데이터를
|
||||
* 가져오기 위해서는 바이트로 변환후 바이트 위치로 가져와야 된다
|
||||
*
|
||||
* @param line 문자열, 한라인
|
||||
* @param start 문자열의 시작 인텍스 (0부터 시작으로)
|
||||
* @param end 문자열의 끝 인덱스
|
||||
*/
|
||||
public static String getStringByte(String line , int start, int end)
|
||||
{
|
||||
byte[] bTmpLine = line.getBytes();
|
||||
|
||||
return new String(bTmpLine, start, end-start);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue