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.util.ByteSequence; |
---|
23 | import org.apache.james.mime4j.util.ContentUtil; |
---|
24 | import org.apache.james.mime4j.util.MimeUtil; |
---|
25 | |
---|
26 | /** |
---|
27 | * The basic immutable MIME field. |
---|
28 | */ |
---|
29 | public final class RawField { |
---|
30 | |
---|
31 | private final ByteSequence raw; |
---|
32 | private final int delimiterIdx; |
---|
33 | private final String name; |
---|
34 | private final String body; |
---|
35 | |
---|
36 | RawField(ByteSequence raw, int delimiterIdx, String name, String body) { |
---|
37 | if (name == null) { |
---|
38 | throw new IllegalArgumentException("Field may not be null"); |
---|
39 | } |
---|
40 | this.raw = raw; |
---|
41 | this.delimiterIdx = delimiterIdx; |
---|
42 | this.name = name.trim(); |
---|
43 | this.body = body; |
---|
44 | } |
---|
45 | |
---|
46 | public RawField(String name, String body) { |
---|
47 | this(null, -1, name, body); |
---|
48 | } |
---|
49 | |
---|
50 | public ByteSequence getRaw() { |
---|
51 | return raw; |
---|
52 | } |
---|
53 | |
---|
54 | public String getName() { |
---|
55 | return name; |
---|
56 | } |
---|
57 | |
---|
58 | public String getBody() { |
---|
59 | if (body != null) { |
---|
60 | return body; |
---|
61 | } |
---|
62 | if (raw != null) { |
---|
63 | int len = raw.length(); |
---|
64 | int off = delimiterIdx + 1; |
---|
65 | if (len > off + 1 && (raw.byteAt(off) & 0xff) == 0x20) off++; |
---|
66 | return MimeUtil.unfold(ContentUtil.decode(raw, off, len - off)); |
---|
67 | } |
---|
68 | return null; |
---|
69 | } |
---|
70 | |
---|
71 | int getDelimiterIdx() { |
---|
72 | return delimiterIdx; |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | public String toString() { |
---|
77 | if (raw != null) { |
---|
78 | return ContentUtil.decode(raw); |
---|
79 | } else { |
---|
80 | StringBuilder buf = new StringBuilder(); |
---|
81 | buf.append(name); |
---|
82 | buf.append(": "); |
---|
83 | if (body != null) { |
---|
84 | buf.append(body); |
---|
85 | } |
---|
86 | return buf.toString(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|