Java URI from toURI(final String location)

Here you can find the source of toURI(final String location)

Description

to URI

License

Apache License

Declaration

public static URI toURI(final String location)
            throws URISyntaxException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static URI toURI(final String location)
            throws URISyntaxException {
        return new URI(replace(location, " ", "%20"));
    }//from www  .  j a va2  s  .co m

    public static URI toURI(final URL url) throws URISyntaxException {
        return toURI(url.toString());
    }

    private static String replace(final String inString,
            final String oldPattern, final String newPattern) {
        if (inString == null) {
            return null;
        }
        if (oldPattern == null || newPattern == null) {
            return inString;
        }

        final StringBuilder sbuf = new StringBuilder();
        // output StringBuilder we'll build up
        int pos = 0;// our position in the old string
        int index = inString.indexOf(oldPattern);
        // the index of an occurrence we've found, or -1
        final int patLen = oldPattern.length();
        while (index >= 0) {
            sbuf.append(inString.substring(pos, index));
            sbuf.append(newPattern);
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
        }
        sbuf.append(inString.substring(pos));

        // remember to append any characters to the right of a match
        return sbuf.toString();
    }
}

Related

  1. toURI(CharSequence uriStr)
  2. toURI(File f)
  3. toUri(File folder)
  4. toURI(final java.io.File file, final StringBuilder builder)
  5. toURI(final String path)
  6. toURI(final String uriString)
  7. toUri(java.io.File file)
  8. toUri(String endpoint, Protocol protocol)