View Javadoc

1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.message;
17  
18  import java.io.InputStream;
19  import java.io.Reader;
20  import java.nio.ByteBuffer;
21  import java.nio.CharBuffer;
22  import java.nio.charset.Charset;
23  import java.util.Iterator;
24  
25  import net.sf.beep4j.Message;
26  import net.sf.beep4j.internal.util.ByteBufferInputStream;
27  import net.sf.beep4j.internal.util.CharSequenceReader;
28  
29  public class DefaultMessage implements Message {
30  	
31  	private final ByteBuffer content;
32  	
33  	private final MessageHeader header;
34  	
35  	private ByteBuffer buffer;
36  	
37  	public DefaultMessage(MessageHeader header, ByteBuffer content) {
38  		this.header = header;
39  		this.content = content;
40  	}
41  		
42  	public String getContentType() {
43  		return header.getContentType();
44  	}
45  	
46  	protected String getCharsetName() {
47  		return header.getCharset();
48  	}
49  	
50  	protected String getTransferEncoding() {
51  		return header.getTransferEncoding();
52  	}
53  	
54  	public Iterator<String> getHeaderNames() {
55  		return header.getHeaderNames();
56  	}
57  	
58  	public String getHeader(String name) {
59  		return header.getHeader(name);
60  	}
61  	
62  	public InputStream getInputStream() {
63  		ByteBuffer buffer = content.asReadOnlyBuffer();
64  		return new ByteBufferInputStream(buffer);
65  	}
66  	
67  	public Reader getReader() {
68  		if (getCharsetName() == null) {
69  			throw new IllegalStateException("no charset has been defined, "
70  					+ "use method with charset parameter");
71  		}
72  		Charset charset = Charset.forName(getCharsetName());
73  		return getReader(charset);
74  	}
75  	
76  	public Reader getReader(String charsetName) {
77  		Charset charset = Charset.forName(charsetName);
78  		return getReader(charset);
79  	}
80  	
81  	private Reader getReader(Charset charset) {
82  		CharBuffer buffer = charset.decode(content.asReadOnlyBuffer());
83  		return new CharSequenceReader(buffer);
84  	}
85  	
86  	public ByteBuffer getContentBuffer() {
87  		return content.asReadOnlyBuffer();
88  	}
89  	
90  	public synchronized ByteBuffer asByteBuffer() {
91  		if (buffer == null) {
92  			ByteBuffer header = this.header.asByteBuffer();
93  			ByteBuffer content = this.content.asReadOnlyBuffer();
94  			buffer = ByteBuffer.allocate(header.remaining() + content.remaining());
95  			buffer.put(header);
96  			buffer.put(content);
97  			buffer.flip();
98  		}
99  		return buffer.asReadOnlyBuffer();
100 	}
101 	
102 	@Override
103 	public boolean equals(Object obj) {
104 		if (this == obj) {
105 			return true;
106 		} else if (obj == null) {
107 			return false;
108 		} else if (obj instanceof Message) {
109 			Message m = (Message) obj;
110 			return asByteBuffer().equals(m.asByteBuffer());
111 		} else {
112 			return false;
113 		}
114 	}
115 	
116 }