Java URI Value Check isAvailable(URI address)

Here you can find the source of isAvailable(URI address)

Description

is Available

License

Apache License

Declaration

public static boolean isAvailable(URI address) 

Method Source Code


//package com.java2s;
/*/*from  w  w w  .ja  va 2 s  .  c  om*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

import java.io.IOException;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.UnknownHostException;

public class Main {
    public static boolean isAvailable(URI address) {
        // Create a socket with a timeout
        try {
            // exclude message box URL from availability check.
            // if(addressString.indexOf("MsgBox")>0) return true;
            // How to parse the address to port
            InetAddress addr = InetAddress.getByName(address.getHost());
            int port = address.getPort();
            if (port == -1) { // URI has no port, invalid URI. Here I choose not
                // to try port 80.
                return false;
            }
            SocketAddress sockaddr = new InetSocketAddress(addr, port);

            // Create an unbound socket
            Socket sock = new Socket();

            // This method will block no more than timeoutMs.
            // If the timeout occurs, SocketTimeoutException is thrown.
            int timeoutMs = 1000; // 2 seconds
            sock.connect(sockaddr, timeoutMs);
            sock.close();
            // System.out.println("Still
            // availabe:"+address.getHost()+":"+address.getPort());
        } catch (UnknownHostException e) {
            // e.printStackTrace();
            return false;
        } catch (SocketTimeoutException e) {
            // e.printStackTrace();
            return false;
        } catch (IOException e) {
            // e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. isAbsoluteURI(String path)
  2. isAbsoluteURI(String uri)
  3. isAbsoluteUri(String uriAsString)
  4. isAnyAddress(final URI u)
  5. isBagUri(final URI uri)
  6. isBase(URI base, URI uri)
  7. isDefaultPort(URI uri)
  8. isDirectory(URI orig)