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.http2.DefaultHttp2DataFrame;
024import io.netty.handler.codec.http2.Http2DataFrame;
025
026/**
027 * An {@link AbstractByteBufBackedChannelOutboundInvokingOutputStream}
028 * that writes {@link Http2DataFrame} messages.
029 *
030 * @author <a href="https://about.me/lairdnelson"
031 * target="_parent">Laird Nelson</a>
032 *
033 * @see #createMessage(ByteBuf)
034 *
035 * @see #createLastMessage()
036 */
037public class ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream extends AbstractByteBufBackedChannelOutboundInvokingOutputStream<Http2DataFrame> {
038
039
040  /*
041   * Constructors.
042   */
043
044  
045  /**
046   * Creates a new {@link
047   * ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream}.
048   *
049   * @param channelOutboundInvoker the {@link ChannelOutboundInvoker}
050   * to which operations are adapted; must not be {@code null}
051   *
052   * @param closeChannelOutboundInvoker whether {@link
053   * ChannelOutboundInvoker#close(ChannelPromise)} will be called on
054   * the supplied {@link ChannelOutboundInvoker} when {@link #close()
055   * close()} is called
056   *
057   * @see
058   * #ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream(ChannelOutboundInvoker,
059   * int, boolean,
060   * AbstractByteBufBackedChannelOutboundInvokingOutputStream.ByteBufCreator)
061   */
062  public ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream(final ChannelOutboundInvoker channelOutboundInvoker,
063                                                                        final boolean closeChannelOutboundInvoker) {
064    this(channelOutboundInvoker, Integer.MAX_VALUE, closeChannelOutboundInvoker, null);
065  }
066
067  /**
068   * Creates a new {@link
069   * ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream}.
070   *
071   * @param channelOutboundInvoker the {@link ChannelOutboundInvoker}
072   * to which operations are adapted; must not be {@code null}
073   *
074   * @param flushThreshold the minimum number of bytes that this
075   * instance has to {@linkplain #write(byte[], int, int) write}
076   * before an automatic {@linkplain #flush() flush} will take place;
077   * if less than {@code 0} {@code 0} will be used instead; if {@code
078   * Integer#MAX_VALUE} then no automatic flushing will occur
079   *
080   * @param closeChannelOutboundInvoker whether {@link
081   * ChannelOutboundInvoker#close(ChannelPromise)} will be called on
082   * the supplied {@link ChannelOutboundInvoker} when {@link #close()
083   * close()} is called
084   *
085   * @param byteBufCreator a {@link ByteBufCreator} that will be used
086   * to {@linkplain ByteBufCreator#toByteBuf(byte[], int, int) create
087   * <code>ByteBuf</code> instances}; may be {@code null} in which
088   * case a default {@link ByteBufCreator} adapting {@link
089   * io.netty.buffer.Unpooled#wrappedBuffer(byte[], int, int)} will be
090   * used instead
091   */
092  public ByteBufBackedChannelOutboundInvokingHttp2DataFrameOutputStream(final ChannelOutboundInvoker channelOutboundInvoker,
093                                                                        final int flushThreshold,
094                                                                        final boolean closeChannelOutboundInvoker,
095                                                                        final ByteBufCreator byteBufCreator) {
096    super(channelOutboundInvoker, flushThreshold, closeChannelOutboundInvoker, byteBufCreator);
097  }
098
099
100  /*
101   * Instance methods.
102   */
103
104
105  /**
106   * Returns a new, empty {@link DefaultHttp2DataFrame} when
107   * invoked.
108   *
109   * <p>This method never returns {@code null}.</p>
110   *
111   * @return a new, empty {@link DefaultHttp2DataFrame}
112   *
113   * @see #close()
114   */
115  @Override
116  protected final Http2DataFrame createLastMessage() {
117    return new DefaultHttp2DataFrame(true);
118  }
119
120  /**
121   * Returns a new {@link DefaultHttp2DataFrame} whose {@link
122   * DefaultHttp2DataFrame#content() content()} method returns the
123   * supplied {@link ByteBuf}.
124   *
125   * <p>This method never returns {@code null}.</p>
126   *
127   * @param content a {@link ByteBuf}; must not be {@code null}
128   *
129   * @return a new {@link DefaultHttp2DataFrame} whose {@link
130   * DefaultHttp2DataFrame#content() content()} method returns the
131   * supplied {@link ByteBuf}; never {@code null}
132   */
133  @Override
134  protected final Http2DataFrame createMessage(final ByteBuf content) {
135    return new DefaultHttp2DataFrame(content);
136  }
137  
138}