1 15 16 package org.enhydra.xml; 17 18 import java.io.*; 19 import java.util.*; 20 21 27 28 public class Utils { 29 30 public static String handleQuotedString( String quotedString ) { 31 String retVal = quotedString; 32 if ( ( retVal.startsWith( "'" ) && retVal.endsWith( "'" ) ) ) { 33 if ( !retVal.equals( "''" ) ) { 34 retVal = retVal.substring( retVal.indexOf( "'" ) + 1, 35 retVal.lastIndexOf( "'" ) ); 36 } 37 else { 38 retVal = ""; 39 } 40 } 41 return retVal; 42 } 43 44 51 public static String replaceAll( String input, String forReplace, 52 String replaceWith ) { 53 StringBuffer result = new StringBuffer (); 54 boolean hasMore = true; 55 while ( hasMore ) { 56 int start = input.indexOf( forReplace ); 57 int end = start + forReplace.length(); 58 if ( start != -1 ) { 59 result.append( input.substring( 0, start ) + replaceWith ); 60 input = input.substring( end ); 61 } 62 else { 63 hasMore = false; 64 result.append( input ); 65 } 66 } 67 if ( result.toString().equals( "" ) ) 68 return input; else 70 return result.toString(); 71 } 72 73 75 80 public static String bytesToHexString( byte[] b ) { 81 String hexString = null; 82 try { 83 if ( b != null ) { 84 ByteArrayInputStream is = new ByteArrayInputStream( b ); 85 hexString = streamToHexString( is ); 86 return hexString; 87 } 88 else { 89 return null; 90 } 91 } 92 catch ( Exception e ) { 93 } 94 return hexString; 95 } 96 97 102 public static byte[] hexStringToBytes( String val ) { 103 byte[] buf = new byte[val.length() / 2]; 104 final char[] hexBytes = { 105 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 106 'E', 'F' 107 }; 108 byte[] hexMap = new byte[256]; 109 for ( int i = 0; i < hexBytes.length; i++ ) { 110 hexMap[hexBytes[i]] = ( byte ) i; 111 } 112 int pos = 0; 113 for ( int i = 0; i < buf.length; i++ ) { 114 buf[i] = ( byte ) ( hexMap[val.charAt( pos++ )] << 4 ); 115 buf[i] += hexMap[val.charAt( pos++ )]; 116 } 117 return buf; 118 } 119 120 126 public static String streamToHexString( InputStream is ) throws IOException { 127 try { 128 char[] hexBytes = { 129 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 130 'E', 'F'}; 131 int c; 132 StringBuffer hexString = new StringBuffer (); 133 while ( ( c = is.read() ) >= 0 ) { 134 hexString.append( hexBytes[ ( c >> 4 ) & 0xf] ); 135 hexString.append( hexBytes[c & 0xf] ); 136 } 137 return hexString.toString(); 138 } 139 catch ( Exception e ) { 140 throw new IOException( e.getMessage() ); 141 } 142 } 143 144 } | Popular Tags |