< prev index next >

test/jdk/java/net/httpclient/http2/server/BodyOutputStream.java

Print this page


   1 /*
   2  * Copyright (c) 2016, 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.common.ByteBufferReference;
  28 import jdk.incubator.http.internal.common.Queue;
  29 import jdk.incubator.http.internal.frame.DataFrame;
  30 
  31 /**
  32  * OutputStream. Incoming window updates handled by the main connection
  33  * reader thread.
  34  */
  35 @SuppressWarnings({"rawtypes","unchecked"})
  36 class BodyOutputStream extends OutputStream {
  37     final static byte[] EMPTY_BARRAY = new byte[0];
  38 
  39     final int streamid;
  40     int window;
  41     boolean closed;
  42     boolean goodToGo = false; // not allowed to send until headers sent
  43     final Http2TestServerConnection conn;
  44     final Queue outputQ;
  45 
  46     BodyOutputStream(int streamid, int initialWindow, Http2TestServerConnection conn) {
  47         this.window = initialWindow;
  48         this.streamid = streamid;
  49         this.conn = conn;
  50         this.outputQ = conn.outputQ;
  51         conn.registerStreamWindowUpdater(streamid, this::updateWindow);
  52     }
  53 
  54     // called from connection reader thread as all incoming window
  55     // updates are handled there.
  56     synchronized void updateWindow(int update) {
  57         window += update;
  58         notifyAll();
  59     }
  60 
  61     void waitForWindow(int demand) throws InterruptedException {


  83         if (closed) {
  84             throw new IOException("closed");
  85         }
  86 
  87         if (!goodToGo) {
  88             throw new IllegalStateException("sendResponseHeaders must be called first");
  89         }
  90         try {
  91             waitForWindow(len);
  92             send(buf, offset, len, 0);
  93         } catch (InterruptedException ex) {
  94             throw new IOException(ex);
  95         }
  96     }
  97 
  98     private void send(byte[] buf, int offset, int len, int flags) throws IOException {
  99         ByteBuffer buffer = ByteBuffer.allocate(len);
 100         buffer.put(buf, offset, len);
 101         buffer.flip();
 102         assert streamid != 0;
 103         DataFrame df = new DataFrame(streamid, flags, ByteBufferReference.of(buffer));
 104         outputQ.put(df);
 105     }
 106 
 107     byte[] one = new byte[1];
 108 
 109     @Override
 110     public void write(int b) throws IOException {
 111         one[0] = (byte) b;
 112         write(one, 0, 1);
 113     }
 114 
 115     void closeInternal() {
 116         closed = true;
 117     }
 118 
 119     @Override
 120     public void close() {
 121         if (closed) {
 122             return;
 123         }
 124         closed = true;

 125         try {
 126             send(EMPTY_BARRAY, 0, 0, DataFrame.END_STREAM);
 127         } catch (IOException ex) {
 128             System.err.println("TestServer: OutputStream.close exception: " + ex);
 129             ex.printStackTrace();
 130         }
 131     }
 132 }
   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 {


  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 }
< prev index next >