1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.jdkim.canon;
21
22 import java.io.FilterOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26
27
28
29
30 public class RelaxedBodyCanonicalizer extends FilterOutputStream {
31
32 private boolean pendingSpaces;
33
34 public RelaxedBodyCanonicalizer(OutputStream out) {
35 super(new SimpleBodyCanonicalizer(out));
36 pendingSpaces = false;
37 }
38
39 public void write(byte[] buffer, int off, int len) throws IOException {
40 int start = off;
41 int end = len + off;
42 for (int k = off; k < end; k++) {
43 if (pendingSpaces) {
44 if (buffer[k] != ' ' && buffer[k] != '\t') {
45 if (buffer[k] != '\r')
46 out.write(' ');
47 pendingSpaces = false;
48 len = len - k + start;
49 start = k;
50 }
51 } else {
52 if (buffer[k] == ' ' || buffer[k] == '\t') {
53 if (k + 1 < end && buffer[k] == ' ' && buffer[k + 1] != ' '
54 && buffer[k + 1] != '\t' && buffer[k + 1] != '\r') {
55
56
57 } else {
58
59 out.write(buffer, start, k - start);
60 pendingSpaces = true;
61 }
62 }
63 }
64 }
65 if (!pendingSpaces) {
66 out.write(buffer, start, len);
67 }
68 }
69
70 public void write(int b) throws IOException {
71 if (pendingSpaces) {
72 if (b != ' ' && b != '\t') {
73 if (b != '\r')
74 out.write(' ');
75 pendingSpaces = false;
76 out.write(b);
77 }
78 } else {
79 if (b == ' ' || b == '\t') {
80 pendingSpaces = true;
81 } else {
82 out.write(b);
83 }
84 }
85 }
86
87 public void close() throws IOException {
88 complete();
89 super.close();
90 }
91
92
93
94
95 private void complete() throws IOException {
96 if (pendingSpaces)
97 out.write(' ');
98 }
99
100 }