Path: blob/master/test/jdk/java/net/URLConnection/B5052093.java
41149 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 505209326* @modules java.base/sun.net.www java.base/sun.net.www.protocol.file27* @library ../../../sun/net/www/httptest/28* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction29* @run main B505209330* @summary URLConnection doesn't support large files31*/32import java.net.*;33import java.io.*;34import sun.net.www.protocol.file.FileURLConnection;35import static java.net.Proxy.NO_PROXY;3637public class B5052093 implements HttpCallback {38private static TestHttpServer server;39private static long testSize = ((long) (Integer.MAX_VALUE)) + 2;4041public static class LargeFile extends File {42public LargeFile() {43super("/dev/zero");44}4546public long length() {47return testSize;48}49}5051public static class LargeFileURLConnection extends FileURLConnection {52public LargeFileURLConnection(LargeFile f) throws IOException {53super(new URL("file:///dev/zero"), f);54}55}5657public void request(HttpTransaction req) {58try {59req.setResponseHeader("content-length", Long.toString(testSize));60req.sendResponse(200, "OK");61} catch (IOException e) {62e.printStackTrace();63}64}6566public static void main(String[] args) throws Exception {67InetAddress loopback = InetAddress.getLoopbackAddress();68server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0);69try {70URL url = new URL("http://" + server.getAuthority() + "/foo");71URLConnection conn = url.openConnection(NO_PROXY);72int i = conn.getContentLength();73long l = conn.getContentLengthLong();74if (i != -1 || l != testSize) {75System.out.println("conn.getContentLength = " + i);76System.out.println("conn.getContentLengthLong = " + l);77System.out.println("testSize = " + testSize);78throw new RuntimeException("Wrong content-length from http");79}8081URLConnection fu = new LargeFileURLConnection(new LargeFile());82i = fu.getContentLength();83l = fu.getContentLengthLong();84if (i != -1 || l != testSize) {85System.out.println("fu.getContentLength = " + i);86System.out.println("fu.getContentLengthLong = " + l);87System.out.println("testSize = " + testSize);88throw new RuntimeException("Wrong content-length from file");89}90} finally {91server.terminate();92}93}94}959697