Here you can find the source of toURI(String location)
public static URI toURI(String location) throws URISyntaxException
//package com.java2s; /*//from w ww.j a v a 2s. c om * Copyright 2010 Red Hat, Inc. and/or its affiliates. * * Licensed 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URI; import java.net.URISyntaxException; import static java.lang.Character.isWhitespace; public class Main { public static URI toURI(String location) throws URISyntaxException { return new URI(replace(location, " ", "%20")); } /** * Replace all occurences of a substring within a string with * another string. * @param inString String to examine * @param oldPattern String to replace * @param newPattern String to insert * @return a String with the replacements * * Borrowed from Spring, under the ASL2.0 license. */ public static String replace(String inString, String oldPattern, String newPattern) { if (isEmpty(inString) || isEmpty(oldPattern) || newPattern == null) { return inString; } 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 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(); } /** * <p>Checks if a String is empty ("") or null.</p> * * <pre> * StringUtils.isEmpty(null) = true * StringUtils.isEmpty("") = true * StringUtils.isEmpty(" ") = false * StringUtils.isEmpty("bob") = false * StringUtils.isEmpty(" bob ") = false * </pre> * * <p>NOTE: This method changed in Lang version 2.0. * It no longer trims the String. * That functionality is available in isBlank().</p> * * @param str the String to check, may be null * @return <code>true</code> if the String is empty or null */ public static boolean isEmpty(final CharSequence str) { if (str == null || str.length() == 0) { return true; } for (int i = 0, length = str.length(); i < length; i++) { if (!isWhitespace(str.charAt(i))) { return false; } } return true; } public static String toString(Reader reader) throws IOException { if (reader instanceof BufferedReader) { return toString((BufferedReader) reader); } else { return toString(new BufferedReader(reader)); } } public static String toString(InputStream is) throws IOException { return toString(new BufferedReader(new InputStreamReader(is, "UTF-8"))); } public static String toString(BufferedReader reader) throws IOException { StringBuilder sb = new StringBuilder(); try { String line; boolean previousLine = false; while ((line = reader.readLine()) != null) { if (previousLine) { sb.append("\n"); } sb.append(line); previousLine = true; } } finally { reader.close(); } return sb.toString(); } }