KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > tools > ByteArrayTool


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.tools;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Tool to manipulate byte arrays
30  * Implementations is quite slow at the momment...
31  * @author Jean-Francois POUX
32  */

33 public class ByteArrayTool {
34
35     // this is slow ...
36
public static byte[] replaceBytes(byte[] src, byte[] pattern, byte[] replace) {
37         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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++) { //starts at j=1!
44
if (i + pattern.length > src.length) { //don't loop anymore
45
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); //copy to begin of pattern
54
if (found) { // add pattern
55
bos.write(replace, 0, replace.length);
56                     i += pattern.length - 1; // move src forward
57
start = i + 1;
58                 } else
59                     start = i;
60             }
61         }
62         bos.write(src, start, i - start);
63         return bos.toByteArray();
64     }
65
66     // check for any LF, if it is no preceeded with CR, add CR
67
// slow impl ...
68
public static byte[] crlfFix(byte[] src) {
69         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc<byte[]> split (byte[] input, byte separator) {
85         if (input==null)
86             return null;
87         
88         List JavaDoc<byte[]> lst = new ArrayList JavaDoc<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         /*
107         for (int i = toCheck.length-1; i ==0; i--) {
108             if (toCheck[i]==pattern[0]) { // look for first byte of pattern
109                 int offset = toCheck.length-i;
110                 if (offset>=pattern.length) {
111                     for (int j = 0; j < pattern.length; j++) {
112                         byte b = pattern[j];
113                         
114                     }
115                 }
116             }
117             
118         }
119         */

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