1 /*
2  * Copyright (c) 2007, 2012, 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 /*
25  * @test
26  * @bug 5052093
27  * @modules java.base/sun.net.www java.base/sun.net.www.protocol.file
28  * @library ../../../sun/net/www/httptest/
29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
30  * @run main B5052093
31  * @summary URLConnection doesn't support large files
32  */
33 import java.net.*;
34 import java.io.*;
35 import sun.net.www.protocol.file.FileURLConnection;
36 
37 public class B5052093 implements HttpCallback {
38     private static TestHttpServer server;
39     private static long testSize = ((long) (Integer.MAX_VALUE)) + 2;
40 
41     public static class LargeFile extends File {
LargeFile()42         public LargeFile() {
43             super("/dev/zero");
44         }
45 
length()46         public long length() {
47             return testSize;
48         }
49     }
50 
51     public static class LargeFileURLConnection extends FileURLConnection {
LargeFileURLConnection(LargeFile f)52         public LargeFileURLConnection(LargeFile f) throws IOException {
53                 super(new URL("file:///dev/zero"), f);
54         }
55     }
56 
request(HttpTransaction req)57     public void request(HttpTransaction req) {
58         try {
59             req.setResponseHeader("content-length", Long.toString(testSize));
60             req.sendResponse(200, "OK");
61         } catch (IOException e) {
62             e.printStackTrace();
63         }
64     }
65 
main(String[] args)66     public static void main(String[] args) throws Exception {
67         server = new TestHttpServer(new B5052093(), 1, 10, 0);
68         try {
69             URL url = new URL("http://localhost:"+server.getLocalPort()+"/foo");
70             URLConnection conn = url.openConnection();
71             int i = conn.getContentLength();
72             long l = conn.getContentLengthLong();
73             if (i != -1 || l != testSize) {
74                 System.out.println("conn.getContentLength = " + i);
75                 System.out.println("conn.getContentLengthLong = " + l);
76                 System.out.println("testSize = " + testSize);
77                 throw new RuntimeException("Wrong content-length from http");
78             }
79 
80             URLConnection fu = new LargeFileURLConnection(new LargeFile());
81             i = fu.getContentLength();
82             l = fu.getContentLengthLong();
83             if (i != -1 || l != testSize) {
84                 System.out.println("fu.getContentLength = " + i);
85                 System.out.println("fu.getContentLengthLong = " + l);
86                 System.out.println("testSize = " + testSize);
87                 throw new RuntimeException("Wrong content-length from file");
88             }
89         } finally {
90             server.terminate();
91         }
92     }
93 }
94