src/jdk.httpserver/share/classes/sun/net/httpserver/ChunkedOutputStream.java
author chegar
Wed, 20 Jun 2018 09:05:57 -0700
changeset 50681 4254bed3c09d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8204679: HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a class which allows the caller to write an arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * number of bytes to an underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * normal close() does not close the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class is buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Each chunk is written in one go as :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * abcd\r\nxxxxxxxxxxxxxx\r\n
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * abcd is the chunk-size, and xxx is the chunk data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * If the length is less than 4 chars (in size) then the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * is written with an offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Final chunk is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * 0\r\n\r\n
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class ChunkedOutputStream extends FilterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* max. amount of user data per chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    final static int CHUNK_SIZE = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* allow 4 bytes for chunk-size plus 4 for CRLFs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final static int OFFSET = 6; /* initial <=4 bytes for len + CRLF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int pos = OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private byte[] buf = new byte [CHUNK_SIZE+OFFSET+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    ExchangeImpl t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    ChunkedOutputStream (ExchangeImpl t, OutputStream src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super (src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.t = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new StreamClosedException ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        buf [pos++] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (count == CHUNK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            writeChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents: 2
diff changeset
    76
        assert count < CHUNK_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void write (byte[]b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new StreamClosedException ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int remain = CHUNK_SIZE - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (len > remain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.arraycopy (b,off,buf,pos,remain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            count = CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            writeChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            len -= remain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            off += remain;
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents: 2
diff changeset
    90
            while (len >= CHUNK_SIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                System.arraycopy (b,off,buf,OFFSET,CHUNK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                len -= CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                off += CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                count = CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                writeChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.arraycopy (b,off,buf,pos,len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents: 2
diff changeset
   103
        if (count == CHUNK_SIZE) {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents: 2
diff changeset
   104
            writeChunk();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents: 2
diff changeset
   105
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * write out a chunk , and reset the pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * chunk does not have to be CHUNK_SIZE bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * count must == number of user bytes (<= CHUNK_SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private void writeChunk () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        char[] c = Integer.toHexString (count).toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int clen = c.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int startByte = 4 - clen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (i=0; i<clen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            buf[startByte+i] = (byte)c[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        buf[startByte + (i++)] = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        buf[startByte + (i++)] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        buf[startByte + (i++) + count] = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        buf[startByte + (i++) + count] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        out.write (buf, startByte, i+count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        pos = OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            /* write an empty chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            writeChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            LeftOverInputStream is = t.getOriginalInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (!is.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* some clients close the connection before empty chunk is sent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        WriteFinishedEvent e = new WriteFinishedEvent (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        t.getHttpContext().getServerImpl().addEvent (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void flush () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new StreamClosedException ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            writeChunk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}