1 | /**************************************************************** |
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
3 | * or more contributor license agreements. See the NOTICE file * |
4 | * distributed with this work for additional information * |
5 | * regarding copyright ownership. The ASF licenses this file * |
6 | * to you under the Apache License, Version 2.0 (the * |
7 | * "License"); you may not use this file except in compliance * |
8 | * with the License. You may obtain a copy of the License at * |
9 | * * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
11 | * * |
12 | * Unless required by applicable law or agreed to in writing, * |
13 | * software distributed under the License is distributed on an * |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
15 | * KIND, either express or implied. See the License for the * |
16 | * specific language governing permissions and limitations * |
17 | * under the License. * |
18 | ****************************************************************/ |
19 | |
20 | package org.apache.james.jdkim.mailets; |
21 | |
22 | import java.io.FilterOutputStream; |
23 | import java.io.IOException; |
24 | import java.io.OutputStream; |
25 | |
26 | /** |
27 | * Ignore writes until the given sequence is found. |
28 | */ |
29 | public class HeaderSkippingOutputStream extends FilterOutputStream { |
30 | |
31 | boolean inHeaders = true; |
32 | byte[] skipTo = "\r\n\r\n".getBytes(); |
33 | int pos = 0; |
34 | |
35 | public HeaderSkippingOutputStream(OutputStream out) { |
36 | super(out); |
37 | } |
38 | |
39 | public void write(byte[] b, int off, int len) throws IOException { |
40 | if (inHeaders) { |
41 | for (int i = off; i < off + len; i++) { |
42 | if (b[i] == skipTo[pos]) { |
43 | pos++; |
44 | if (pos == skipTo.length) { |
45 | inHeaders = false; |
46 | if (len - i - 1 > 0) |
47 | out.write(b, i + 1, len - i - 1); |
48 | break; |
49 | } |
50 | } else |
51 | pos = 0; |
52 | } |
53 | } else { |
54 | out.write(b, off, len); |
55 | } |
56 | } |
57 | |
58 | public void write(int b) throws IOException { |
59 | if (inHeaders) { |
60 | if (skipTo[pos] == b) { |
61 | pos++; |
62 | if (pos == skipTo.length) |
63 | inHeaders = false; |
64 | } else |
65 | pos = 0; |
66 | } else { |
67 | out.write(b); |
68 | } |
69 | } |
70 | |
71 | } |