View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
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  
17  package org.springframework.ws.transport.xmpp;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  
23  import org.springframework.util.Assert;
24  
25  import org.jivesoftware.smack.packet.Message;
26  
27  /**
28   * Output stream that wraps a {@link Message}.
29   *
30   * @author Gildas Cuisinier
31   * @author Arjen Poutsma
32   * @since 2.0
33   */
34  class MessageOutputStream extends FilterOutputStream {
35  
36      private final Message message;
37  
38      private final String encoding;
39  
40      MessageOutputStream(Message message, String encoding) {
41          super(new ByteArrayOutputStream());
42          Assert.notNull(message, "'message' must not be null");
43          Assert.notNull(encoding, "'encoding' must not be null");
44          this.message = message;
45          this.encoding = encoding;
46      }
47  
48      @Override
49      public void flush() throws IOException {
50          super.flush();
51          ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
52          String text = new String(bos.toByteArray(), encoding);
53          message.setBody(text);
54      }
55  }