1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.nio.ByteBuffer;
  26 
  27 import jdk.incubator.http.internal.frame.DataFrame;
  28 
  29 /**
  30  * OutputStream. Incoming window updates handled by the main connection
  31  * reader thread.
  32  */
  33 @SuppressWarnings({"rawtypes","unchecked"})
  34 class BodyOutputStream extends OutputStream {
  35     final static byte[] EMPTY_BARRAY = new byte[0];
  36 
  37     final int streamid;
  38     int window;
  39     volatile boolean closed;
  40     boolean goodToGo = false; // not allowed to send until headers sent
  41     final Http2TestServerConnection conn;
  42     final Queue outputQ;
  43 
  44     BodyOutputStream(int streamid, int initialWindow, Http2TestServerConnection conn) {
  45         this.window = initialWindow;
  46         this.streamid = streamid;
  47         this.conn = conn;
  48         this.outputQ = conn.outputQ;
  49         conn.registerStreamWindowUpdater(streamid, this::updateWindow);
  50     }
  51 
  52     // called from connection reader thread as all incoming window
  53     // updates are handled there.
  54     synchronized void updateWindow(int update) {
  55         window += update;
  56         notifyAll();
  57     }
  58 
  59     void waitForWindow(int demand) throws InterruptedException {
  60         // first wait for the connection window
  61         conn.obtainConnectionWindow(demand);
  62         // now wait for the stream window
  63         synchronized (this) {
  64             while (demand > 0) {
  65                 int n = Math.min(demand, window);
  66                 demand -= n;
  67                 window -= n;
  68                 if (demand > 0) {
  69                     wait();
  70                 }
  71             }
  72         }
  73     }
  74 
  75     void goodToGo() {
  76         goodToGo = true;
  77     }
  78 
  79     @Override
  80     public void write(byte[] buf, int offset, int len) throws IOException {
  81         if (closed) {
  82             throw new IOException("closed");
  83         }
  84 
  85         if (!goodToGo) {
  86             throw new IllegalStateException("sendResponseHeaders must be called first");
  87         }
  88         try {
  89             waitForWindow(len);
  90             send(buf, offset, len, 0);
  91         } catch (InterruptedException ex) {
  92             throw new IOException(ex);
  93         }
  94     }
  95 
  96     private void send(byte[] buf, int offset, int len, int flags) throws IOException {
  97         ByteBuffer buffer = ByteBuffer.allocate(len);
  98         buffer.put(buf, offset, len);
  99         buffer.flip();
 100         assert streamid != 0;
 101         DataFrame df = new DataFrame(streamid, flags, buffer);
 102         outputQ.put(df);
 103     }
 104 
 105     byte[] one = new byte[1];
 106 
 107     @Override
 108     public void write(int b) throws IOException {
 109         one[0] = (byte) b;
 110         write(one, 0, 1);
 111     }
 112 
 113     void closeInternal() {
 114         closed = true;
 115     }
 116 
 117     @Override
 118     public void close() {
 119         if (closed) return;
 120         synchronized (this) {
 121             if (closed) return;
 122             closed = true;
 123         }
 124         try {
 125             send(EMPTY_BARRAY, 0, 0, DataFrame.END_STREAM);
 126         } catch (IOException ex) {
 127             System.err.println("TestServer: OutputStream.close exception: " + ex);
 128             ex.printStackTrace();
 129         }
 130     }
 131 }