Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URL/GetContent.java
38811 views
/*1* Copyright (c) 1998, 2010, 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 414531526* @summary Test a read from nonexistant URL27*/2829import java.net.*;30import java.io.*;3132public class GetContent implements Runnable {3334ServerSocket ss;3536public void run() {37try {38Socket s = ss.accept();39s.setTcpNoDelay(true);4041PrintStream out = new PrintStream(42new BufferedOutputStream(43s.getOutputStream() ));4445out.print("HTTP/1.1 404 Not Found\r\n");46out.print("Connection: close\r\n");47out.print("Content-Type: text/html; charset=iso-8859-1\r\n");48out.print("\r\n");49out.flush();50out.print("<HTML><BODY>Sorry, page not found</BODY></HTML>");51out.flush();5253// wait for client to read response - otherwise http54// client get error and re-establish connection55Thread.sleep(2000);5657s.close();58} catch (Exception e) {59e.printStackTrace();60} finally {61try { ss.close(); } catch (IOException unused) {}62}63}6465GetContent() throws Exception {6667ss = new ServerSocket(0);68Thread thr = new Thread(this);69thr.start();7071boolean error = true;72try {73String name = "http://localhost:" + ss.getLocalPort() +74"/no-such-name";75java.net.URL url = null;76url = new java.net.URL(name);77Object obj = url.getContent();78InputStream in = (InputStream) obj;79byte buff[] = new byte[200];80int len = in.read(buff);81} catch (IOException ex) {82error = false;83}8485if (error)86throw new RuntimeException("No IOException generated.");87}8889public static void main(String args[]) throws Exception {90new GetContent();91}92}939495