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.mime4j.stream; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.MimeException; |
---|
23 | import org.apache.james.mime4j.io.MaxHeaderLengthLimitException; |
---|
24 | import org.apache.james.mime4j.util.ByteArrayBuffer; |
---|
25 | import org.apache.james.mime4j.util.CharsetUtil; |
---|
26 | |
---|
27 | public class LenientFieldBuilder implements FieldBuilder { |
---|
28 | |
---|
29 | private final ByteArrayBuffer buf; |
---|
30 | private final int maxlen; |
---|
31 | |
---|
32 | |
---|
33 | public LenientFieldBuilder(int maxlen) { |
---|
34 | this.buf = new ByteArrayBuffer(1024); |
---|
35 | this.maxlen = maxlen; |
---|
36 | } |
---|
37 | |
---|
38 | public void reset() { |
---|
39 | this.buf.clear(); |
---|
40 | } |
---|
41 | |
---|
42 | public void append(final ByteArrayBuffer line) throws MaxHeaderLengthLimitException { |
---|
43 | if (line == null) { |
---|
44 | return; |
---|
45 | } |
---|
46 | int len = line.length(); |
---|
47 | if (this.maxlen > 0 && this.buf.length() + len >= this.maxlen) { |
---|
48 | // buffer is over the max limit: quietly ignore all further input |
---|
49 | return; |
---|
50 | } |
---|
51 | if (this.buf == null) { |
---|
52 | } |
---|
53 | int beginIndex = 0; |
---|
54 | int endIndex = len; |
---|
55 | while (beginIndex < endIndex |
---|
56 | && CharsetUtil.isWhitespace((char)(line.byteAt(beginIndex) & 0xff))) { |
---|
57 | beginIndex++; |
---|
58 | } |
---|
59 | while (endIndex > beginIndex |
---|
60 | && CharsetUtil.isWhitespace((char)(line.byteAt(endIndex - 1) & 0xff))) { |
---|
61 | endIndex--; |
---|
62 | } |
---|
63 | if (this.buf.length() > 0) { |
---|
64 | this.buf.append(' '); |
---|
65 | } |
---|
66 | this.buf.append(line.buffer(), beginIndex, endIndex - beginIndex); |
---|
67 | } |
---|
68 | |
---|
69 | public RawField build() throws MimeException { |
---|
70 | if (this.buf == null) { |
---|
71 | return null; |
---|
72 | } |
---|
73 | RawField field = RawFieldParser.DEFAULT.parseField(this.buf); |
---|
74 | // Discard modified raw data |
---|
75 | return new RawField(field.getName(), field.getBody()); |
---|
76 | } |
---|
77 | |
---|
78 | public ByteArrayBuffer getRaw() { |
---|
79 | return null; |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|