source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/main/java/org/apache/james/mime4j/dom/SingleBody.java @ 6785

Revision 6785, 4.9 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.dom;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25
26/**
27 * Abstract implementation of a single message body; that is, a body that does
28 * not contain (directly or indirectly) any other child bodies. It also provides
29 * the parent functionality required by bodies.
30 */
31public abstract class SingleBody implements Body {
32
33    private Entity parent = null;
34
35    /**
36     * Sole constructor.
37     */
38    protected SingleBody() {
39    }
40
41    /**
42     * @see org.apache.james.mime4j.dom.Body#getParent()
43     */
44    public Entity getParent() {
45        return parent;
46    }
47
48    /**
49     * @see org.apache.james.mime4j.dom.Body#setParent(org.apache.james.mime4j.dom.Entity)
50     */
51    public void setParent(Entity parent) {
52        this.parent = parent;
53    }
54
55    /**
56     * Gets a <code>InputStream</code> which reads the bytes of the body.
57     *
58     * @return the stream, transfer decoded
59     * @throws IOException
60     *             on I/O errors.
61     */
62    public abstract InputStream getInputStream() throws IOException;
63
64    /**
65     * Writes this single body to the given stream. The default implementation copies
66     * the input stream obtained by {@link #getInputStream()} to the specified output
67     * stream. May be overwritten by a subclass to improve performance.
68     *
69     * @param out
70     *            the stream to write to.
71     * @throws IOException
72     *             in case of an I/O error
73     */
74    public void writeTo(OutputStream out) throws IOException {
75        if (out == null)
76            throw new IllegalArgumentException();
77
78        InputStream in = getInputStream();
79        SingleBody.copy(in, out);
80        in.close();
81    }
82
83    /**
84     * Returns a copy of this <code>SingleBody</code> (optional operation).
85     * <p>
86     * The general contract of this method is as follows:
87     * <ul>
88     * <li>Invoking {@link #getParent()} on the copy returns <code>null</code>.
89     * That means that the copy is detached from the parent entity of this
90     * <code>SingleBody</code>. The copy may get attached to a different
91     * entity later on.</li>
92     * <li>The underlying content does not have to be copied. Instead it may be
93     * shared between multiple copies of a <code>SingleBody</code>.</li>
94     * <li>If the underlying content is shared by multiple copies the
95     * implementation has to make sure that the content gets deleted when the
96     * last copy gets disposed of (and not before that).</li>
97     * </ul>
98     * <p>
99     * This implementation always throws an
100     * <code>UnsupportedOperationException</code>.
101     *
102     * @return a copy of this <code>SingleBody</code>.
103     * @throws UnsupportedOperationException
104     *             if the <code>copy</code> operation is not supported by this
105     *             single body.
106     */
107    public SingleBody copy() {
108        throw new UnsupportedOperationException();
109    }
110
111    /**
112     * Subclasses should override this method if they have allocated resources
113     * that need to be freed explicitly (e.g. cannot be simply reclaimed by the
114     * garbage collector).
115     *
116     * The default implementation of this method does nothing.
117     *
118     * @see org.apache.james.mime4j.dom.Disposable#dispose()
119     */
120    public void dispose() {
121    }
122
123    static final int DEFAULT_ENCODING_BUFFER_SIZE = 1024;
124   
125    /**
126     * Copies the contents of one stream to the other.
127     * @param in not null
128     * @param out not null
129     * @throws IOException
130     */
131    private static void copy(final InputStream in, final OutputStream out) throws IOException {
132        final byte[] buffer = new byte[DEFAULT_ENCODING_BUFFER_SIZE];
133        int inputLength;
134        while (-1 != (inputLength = in.read(buffer))) {
135            out.write(buffer, 0, inputLength);
136        }
137    }
138
139}
Note: See TracBrowser for help on using the repository browser.