001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2019 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.jersey.netty;
018
019import io.netty.buffer.ByteBuf;
020
021import io.netty.channel.ChannelOutboundInvoker;
022
023import io.netty.handler.codec.http.DefaultLastHttpContent;
024import io.netty.handler.codec.http.DefaultHttpContent;
025import io.netty.handler.codec.http.HttpContent;
026
027/**
028 * An {@link AbstractByteBufBackedChannelOutboundInvokingOutputStream}
029 * that writes {@link HttpContent} messages.
030 *
031 * @author <a href="https://about.me/lairdnelson"
032 * target="_parent">Laird Nelson</a>
033 *
034 * @see #createMessage(ByteBuf)
035 *
036 * @see #createLastMessage()
037 */
038public class ByteBufBackedChannelOutboundInvokingHttpContentOutputStream extends AbstractByteBufBackedChannelOutboundInvokingOutputStream<HttpContent> {
039
040
041  /*
042   * Constructors.
043   */
044
045
046  /**
047   * Creates a new {@link
048   * ByteBufBackedChannelOutboundInvokingHttpContentOutputStream}.
049   *
050   * @param channelOutboundInvoker the {@link ChannelOutboundInvoker}
051   * to which operations are adapted; must not be {@code null}
052   *
053   * @param closeChannelOutboundInvoker whether {@link
054   * ChannelOutboundInvoker#close(ChannelPromise)} will be called on
055   * the supplied {@link ChannelOutboundInvoker} when {@link #close()
056   * close()} is called
057   *
058   * @see
059   * #ByteBufBackedChannelOutboundInvokingHttpContentOutputStream(ChannelOutboundInvoker,
060   * int, boolean,
061   * AbstractByteBufBackedChannelOutboundInvokingOutputStream.ByteBufCreator)
062   */
063  public ByteBufBackedChannelOutboundInvokingHttpContentOutputStream(final ChannelOutboundInvoker channelOutboundInvoker,
064                                                                     final boolean closeChannelOutboundInvoker) {
065    this(channelOutboundInvoker, Integer.MAX_VALUE, closeChannelOutboundInvoker, null);
066  }
067
068  /**
069   * Creates a new {@link
070   * ByteBufBackedChannelOutboundInvokingHttpContentOutputStream}.
071   *
072   * @param channelOutboundInvoker the {@link ChannelOutboundInvoker}
073   * to which operations are adapted; must not be {@code null}
074   *
075   * @param flushThreshold the minimum number of bytes that this
076   * instance has to {@linkplain #write(byte[], int, int) write}
077   * before an automatic {@linkplain #flush() flush} will take place;
078   * if less than {@code 0} {@code 0} will be used instead; if {@code
079   * Integer#MAX_VALUE} then no automatic flushing will occur
080   *
081   * @param closeChannelOutboundInvoker whether {@link
082   * ChannelOutboundInvoker#close(ChannelPromise)} will be called on
083   * the supplied {@link ChannelOutboundInvoker} when {@link #close()
084   * close()} is called
085   *
086   * @param byteBufCreator a {@link ByteBufCreator} that will be used
087   * to {@linkplain ByteBufCreator#toByteBuf(byte[], int, int) create
088   * <code>ByteBuf</code> instances}; may be {@code null} in which
089   * case a default {@link ByteBufCreator} adapting {@link
090   * io.netty.buffer.Unpooled#wrappedBuffer(byte[], int, int)} will be
091   * used instead
092   */
093  public ByteBufBackedChannelOutboundInvokingHttpContentOutputStream(final ChannelOutboundInvoker channelOutboundInvoker,
094                                                                     final int flushThreshold,
095                                                                     final boolean closeChannelOutboundInvoker,
096                                                                     final ByteBufCreator byteBufCreator) {
097    super(channelOutboundInvoker, flushThreshold, closeChannelOutboundInvoker, byteBufCreator);
098  }
099
100
101  /*
102   * Instance methods.
103   */
104
105  
106  /**
107   * Returns a new {@link DefaultHttpContent} whose {@link
108   * DefaultHttpContent#content() content()} method returns the
109   * supplied {@link ByteBuf}.
110   *
111   * <p>This method never returns {@code null}.</p>
112   *
113   * @param content a {@link ByteBuf}; must not be {@code null}
114   *
115   * @return a new {@link DefaultHttpContent} whose {@link
116   * DefaultHttpContent#content() content()} method returns the
117   * supplied {@link ByteBuf}; never {@code null}
118   */
119  @Override
120  protected HttpContent createMessage(final ByteBuf content) {
121    return new DefaultHttpContent(content);
122  }
123
124
125  /**
126   * Returns a new {@link DefaultLastHttpContent} when invoked.
127   *
128   * <p>This method never returns {@code null}.</p>
129   *
130   * @return a new {@link DefaultLastHttpContent}
131   *
132   * @see #close()
133   */
134  @Override
135  protected HttpContent createLastMessage() {
136    return new DefaultLastHttpContent();
137  }
138
139}