1 5 package com.teamkonzept.lib; 6 7 10 public class TKUrlConverter extends TKConverter { 11 public final static String CONV_ID = "URL"; 12 public final static String CONV_NAME = "URL-ISO-8859_1"; 13 14 public String getName() 15 { 16 return CONV_NAME; 17 } 18 19 public int getMaxBytesPerChar() 20 { 21 return 3; 22 } 23 24 public int minCharSize(int byteCount) 25 { 26 return byteCount; 27 } 28 29 public int charsToBytes(char src[], byte dst[], int srcBegin, int length, int dstBegin) 30 { 31 int lastPos = srcBegin+length; 32 int firstPos = dstBegin; 33 for( int i=srcBegin; i<lastPos; i++ ) { 34 char c = src[i]; 35 if( (c >= 'A' && c <= 'Z') 36 || (c >= 'a' && c <= 'z') 37 || (c >= '0' && c <= '9') 38 || c == '@' || c == '*' || c == ':' || c == '-' || c == '_' 39 ) { 40 dst[ dstBegin++ ] = (byte) c; 41 } 42 46 else if( c <= '\u00FF' ) { 47 int code = (int) c; 48 dst[ dstBegin++ ] = (byte) '%'; 49 dst[ dstBegin++ ] = (byte) Character.forDigit( ( code / 16 ), 16 ); 50 dst[ dstBegin++ ] = (byte) Character.forDigit( ( code % 16 ), 16 ); 51 } 52 } 53 54 return dstBegin - firstPos; 55 } 56 57 final static byte PERCENT = (byte) '%'; 58 final static byte PLUS = (byte) '+'; 59 60 public int bytesToChars(byte src[], char dst[], int srcBegin, int length, int dstBegin) 61 { 62 int lastPos = srcBegin+length; 63 int firstPos = dstBegin; 64 for( int i=srcBegin; i<lastPos; i++ ) { 65 byte b = src[i]; 66 if( b == PERCENT ) { 67 int high = Character.digit( (char) src[++i], 16 ); 68 int code = 16*high + Character.digit( (char) src[++i], 16 ); 69 dst[dstBegin++] = (char) code; 70 } 71 else if( b == PLUS ) { 72 dst[dstBegin++] = ' '; 73 } 74 else { 75 dst[dstBegin++] = (char) b; 76 } 77 } 78 return dstBegin - firstPos; 79 } 80 } 81 82 | Popular Tags |