Java Path Concatenate concatPaths(String path1, String path2)

Here you can find the source of concatPaths(String path1, String path2)

Description

Concatenates two path strings.

License

Apache License

Declaration

public static String concatPaths(String path1, String path2) 

Method Source Code

//package com.java2s;
/*//from  w  ww .ja v  a 2  s. c o m
 * Copyright 2016 LINE Corporation
 *
 * LINE Corporation 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.
 */

public class Main {
    /**
     * Concatenates two path strings.
     */
    public static String concatPaths(String path1, String path2) {
        path2 = path2 == null ? "" : path2;

        if (path1 == null || path1.isEmpty() || "/".equals(path1)) {
            if (path2.isEmpty()) {
                return "/";
            }

            if (path2.charAt(0) == '/') {
                return path2; // Most requests will land here.
            }

            return new StringBuilder(path2.length() + 1).append('/')
                    .append(path2).toString();
        }

        // At this point, we are sure path1 is neither empty nor null.
        if (path2.isEmpty()) {
            // Only path1 is non-empty. No need to concatenate.
            return path1;
        }

        if (path1.charAt(path1.length() - 1) == '/') {
            if (path2.charAt(0) == '/') {
                // path1 ends with '/' and path2 starts with '/'.
                // Avoid double-slash by stripping the first slash of path2.
                return new StringBuilder(path1.length() + path2.length()
                        - 1).append(path1).append(path2, 1, path2.length())
                        .toString();
            }

            // path1 ends with '/' and path2 does not start with '/'.
            // Simple concatenation would suffice.
            return new StringBuilder(path1.length() + path2.length())
                    .append(path1).append(path2).toString();
        }

        if (path2.charAt(0) == '/') {
            // path1 does not end with '/' and path2 starts with '/'.
            // Simple concatenation would suffice.
            return path1 + path2;
        }

        // path1 does not end with '/' and path2 does not start with '/'.
        // Need to insert '/' between path1 and path2.
        return new StringBuilder(path1.length() + path2.length() + 1)
                .append(path1).append('/').append(path2).toString();
    }
}

Related

  1. concatPath(String pathName, String name)
  2. concatPath(String root, String fragment)
  3. concatPath(String root, String node)
  4. concatPath(String src, String dst)
  5. concatPath(String... path)
  6. concatPaths(String path1, String path2)
  7. concatPaths(String prefix, String postfix)
  8. concatPathsStrings(String head, String tail)