001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.stream;
021
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.apache.mina.core.buffer.IoBuffer;
026import org.apache.mina.core.session.IoSession;
027
028/**
029 * Filter implementation which makes it possible to write {@link InputStream}
030 * objects directly using {@link IoSession#write(Object)}. When an
031 * {@link InputStream} is written to a session this filter will read the bytes
032 * from the stream into {@link IoBuffer} objects and write those buffers
033 * to the next filter. When end of stream has been reached this filter will
034 * call {@link org.apache.mina.core.filterchain.IoFilter.NextFilter#messageSent(org.apache.mina.core.session.IoSession, org.apache.mina.core.write.WriteRequest)} using the original
035 * {@link InputStream} written to the session and notifies
036 * {@link org.apache.mina.core.future.WriteFuture} on the
037 * original {@link org.apache.mina.core.write.WriteRequest}.
038 * <p>
039 * This filter will ignore written messages which aren't {@link InputStream}
040 * instances. Such messages will be passed to the next filter directly.
041 * <p>
042 * NOTE: this filter does not close the stream after all data from stream
043 * has been written. The {@link org.apache.mina.core.service.IoHandler} should take
044 * care of that in its
045 * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)}
046 * callback.
047 *
048 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
049 * @org.apache.xbean.XBean
050 */
051public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> {
052
053    @Override
054    protected IoBuffer getNextBuffer(InputStream is) throws IOException {
055        byte[] bytes = new byte[getWriteBufferSize()];
056
057        int off = 0;
058        int n = 0;
059        while (off < bytes.length && (n = is.read(bytes, off, bytes.length - off)) != -1) {
060            off += n;
061        }
062
063        if (n == -1 && off == 0) {
064            return null;
065        }
066
067        IoBuffer buffer = IoBuffer.wrap(bytes, 0, off);
068
069        return buffer;
070    }
071
072    @Override
073    protected Class<InputStream> getMessageClass() {
074        return InputStream.class;
075    }
076
077}