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.mina.filter.stream; 21 22 import java.io.IOException; 23 import java.io.InputStream; 24 25 import org.apache.mina.core.buffer.IoBuffer; 26 import org.apache.mina.core.filterchain.IoFilter; 27 import org.apache.mina.core.session.IoSession; 28 import org.apache.mina.core.write.WriteRequest; 29 30 /** 31 * Filter implementation which makes it possible to write {@link InputStream} 32 * objects directly using {@link IoSession#write(Object)}. When an 33 * {@link InputStream} is written to a session this filter will read the bytes 34 * from the stream into {@link IoBuffer} objects and write those buffers 35 * to the next filter. When end of stream has been reached this filter will 36 * call {@link IoFilter.NextFilter#messageSent(IoSession,WriteRequest)} using the original 37 * {@link InputStream} written to the session and notifies 38 * {@link org.apache.mina.core.future.WriteFuture} on the 39 * original {@link org.apache.mina.core.write.WriteRequest}. 40 * <p/> 41 * This filter will ignore written messages which aren't {@link InputStream} 42 * instances. Such messages will be passed to the next filter directly. 43 * </p> 44 * <p/> 45 * NOTE: this filter does not close the stream after all data from stream 46 * has been written. The {@link org.apache.mina.core.service.IoHandler} should take 47 * care of that in its 48 * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)} 49 * callback. 50 * </p> 51 * 52 * @author The Apache MINA Project (dev@mina.apache.org) 53 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 54 */ 55 public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> { 56 57 @Override 58 protected IoBuffer getNextBuffer(InputStream is) throws IOException { 59 byte[] bytes = new byte[getWriteBufferSize()]; 60 61 int off = 0; 62 int n = 0; 63 while (off < bytes.length 64 && (n = is.read(bytes, off, bytes.length - off)) != -1) { 65 off += n; 66 } 67 68 if (n == -1 && off == 0) { 69 return null; 70 } 71 72 IoBuffer buffer = IoBuffer.wrap(bytes, 0, off); 73 74 return buffer; 75 } 76 77 @Override 78 protected Class<InputStream> getMessageClass() { 79 return InputStream.class; 80 } 81 82 }