test/java/net/URLConnection/B5052093.java

Print this page




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


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