1 21 package org.jsmtpd.tools; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 28 33 public class ByteArrayTool { 34 35 public static byte[] replaceBytes(byte[] src, byte[] pattern, byte[] replace) { 37 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 38 int start = 0; 39 int i; 40 for (i = 0; i < src.length; i++) { 41 if (src[i] == pattern[0]) { 42 boolean found = true; 43 for (int j = 0; j < pattern.length; j++) { if (i + pattern.length > src.length) { found = false; 46 break; 47 } 48 if (src[i + j] != pattern[j]) { 49 found = false; 50 break; 51 } 52 } 53 bos.write(src, start, i - start); if (found) { bos.write(replace, 0, replace.length); 56 i += pattern.length - 1; start = i + 1; 58 } else 59 start = i; 60 } 61 } 62 bos.write(src, start, i - start); 63 return bos.toByteArray(); 64 } 65 66 public static byte[] crlfFix(byte[] src) { 69 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 70 for (int i = 0; i < src.length; i++) { 71 if ((src[i] == LF[0]) && (i > 1)) { 72 if (src[i - 1] != CR[0]) 73 bos.write(CR, 0, 1); 74 } 75 bos.write(src[i]); 76 } 77 return bos.toByteArray(); 78 } 79 80 public static boolean compare (byte[] a, byte[] b) { 81 return Arrays.equals(a,b); 82 } 83 84 public static List <byte[]> split (byte[] input, byte separator) { 85 if (input==null) 86 return null; 87 88 List <byte[]> lst = new ArrayList <byte[]>(); 89 int startPos=0; 90 for (int i = 0; i < input.length; i++) { 91 if (input[i]==separator) { 92 byte[] tmp = new byte[i-startPos]; 93 System.arraycopy(input,startPos,tmp,0,i-startPos); 94 lst.add(tmp); 95 startPos=i+1; 96 } 97 } 98 if ((startPos!=0)&&(startPos<input.length)) { 99 byte[] tmp = new byte[input.length-startPos]; 100 System.arraycopy(input,startPos,tmp,0,input.length-startPos); 101 lst.add(tmp); 102 } 103 return lst; 104 } 105 public static int patternAt (byte[] toCheck, byte[] pattern) { 106 120 return -1; 121 } 122 public static final byte[] CRLF = { 13, 10 }; 123 public static final byte[] LF = { 10 }; 124 public static final byte[] CR = { 13 }; 125 public static final byte[] EOM = { 13, 10, 46, 13, 10 }; 126 public static final byte NULL = 0; 127 } | Popular Tags |