Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/B5052093.java
38812 views
/*1* Copyright (c) 2007, 2012, 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* @library ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main B505209329* @summary URLConnection doesn't support large files30*/31import java.net.*;32import java.io.*;33import sun.net.www.protocol.file.FileURLConnection;3435public class B5052093 implements HttpCallback {36private static TestHttpServer server;37private static long testSize = ((long) (Integer.MAX_VALUE)) + 2;3839public static class LargeFile extends File {40public LargeFile() {41super("/dev/zero");42}4344public long length() {45return testSize;46}47}4849public static class LargeFileURLConnection extends FileURLConnection {50public LargeFileURLConnection(LargeFile f) throws IOException {51super(new URL("file:///dev/zero"), f);52}53}5455public void request(HttpTransaction req) {56try {57req.setResponseHeader("content-length", Long.toString(testSize));58req.sendResponse(200, "OK");59} catch (IOException e) {60e.printStackTrace();61}62}6364public static void main(String[] args) throws Exception {65server = new TestHttpServer(new B5052093(), 1, 10, 0);66try {67URL url = new URL("http://localhost:"+server.getLocalPort()+"/foo");68URLConnection conn = url.openConnection();69int i = conn.getContentLength();70long l = conn.getContentLengthLong();71if (i != -1 || l != testSize) {72System.out.println("conn.getContentLength = " + i);73System.out.println("conn.getContentLengthLong = " + l);74System.out.println("testSize = " + testSize);75throw new RuntimeException("Wrong content-length from http");76}7778URLConnection fu = new LargeFileURLConnection(new LargeFile());79i = fu.getContentLength();80l = fu.getContentLengthLong();81if (i != -1 || l != testSize) {82System.out.println("fu.getContentLength = " + i);83System.out.println("fu.getContentLengthLong = " + l);84System.out.println("testSize = " + testSize);85throw new RuntimeException("Wrong content-length from file");86}87} finally {88server.terminate();89}90}91}929394