jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/UpgradeInputStream.java
2026-02-11 13:47:58 -05:00

77 lines
2.5 KiB
Java

/*
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.httpserver;
import java.io.IOException;
import java.io.InputStream;
/**
* A class which allows the caller to read the underlying stream for an upgrade request.
* close() does not close the underlying stream
*/
class UpgradeInputStream extends LeftOverInputStream {
UpgradeInputStream(ExchangeImpl t, InputStream src) {
super(t, src);
}
@Override
protected int readImpl(byte[] b, int off, int len) throws IOException {
if (!t.upgraded) {
return -1;
}
return in.read(b, off, len);
}
@Override
public int available() throws IOException {
if (!t.upgraded) {
return 0;
}
return in.available();
}
@Override
public void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
@Override
public void close() throws IOException {
closed = true;
if(t.getConnection().state == State.REQUEST) {
t.getServerImpl().requestCompleted(t.getConnection());
}
}
@Override
public byte[] readAllBytes() throws IOException {
if (!t.upgraded) {
return new byte[0];
}
throw new IOException("readAllBytes not supported for upgraded requests");
}
}