FileUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.io;

  18. import java.io.File;
  19. import java.net.URL;
  20. import java.nio.ByteBuffer;
  21. import java.nio.charset.Charset;
  22. import java.nio.charset.StandardCharsets;

  23. /**
  24.  * This class is a subset of org.apache.commons.io.FileUtils, git-svn-id:
  25.  * https://svn.apache.org/repos/asf/commons/proper/io/trunk@1423916 13f79535-47bb-0310-9956-ffa450edef68. The subset is
  26.  * determined by {@link FileLocatorUtils}. The copied constants and methods are <em>literally</em> copied.
  27.  *
  28.  * See CONFIGURATION-521 for a discussion.
  29.  */
  30. final class FileUtils {
  31.     /**
  32.      * The UTF-8 character set, used to decode octets in URLs.
  33.      */
  34.     private static final Charset UTF8 = StandardCharsets.UTF_8;

  35.     /**
  36.      * Decodes the specified URL as per RFC 3986, i.e. transforms percent-encoded octets to characters by decoding with the
  37.      * UTF-8 character set. This function is primarily intended for usage with {@link java.net.URL} which unfortunately does
  38.      * not enforce proper URLs. As such, this method will leniently accept invalid characters or malformed percent-encoded
  39.      * octets and simply pass them literally through to the result string. Except for rare edge cases, this will make
  40.      * unencoded URLs pass through unaltered.
  41.      *
  42.      * @param url The URL to decode, may be {@code null}.
  43.      * @return The decoded URL or {@code null} if the input was {@code null}.
  44.      */
  45.     static String decodeUrl(final String url) {
  46.         String decoded = url;
  47.         if (url != null && url.indexOf('%') >= 0) {
  48.             final int n = url.length();
  49.             final StringBuilder buffer = new StringBuilder();
  50.             final ByteBuffer bytes = ByteBuffer.allocate(n);
  51.             for (int i = 0; i < n;) {
  52.                 if (url.charAt(i) == '%') {
  53.                     try {
  54.                         do {
  55.                             final byte octet = (byte) Integer.parseInt(url.substring(i + 1, i + 3), 16);
  56.                             bytes.put(octet);
  57.                             i += 3;
  58.                         } while (i < n && url.charAt(i) == '%');
  59.                         continue;
  60.                     } catch (final RuntimeException ignored) {
  61.                         // malformed percent-encoded octet, fall through and
  62.                         // append characters literally
  63.                     } finally {
  64.                         if (bytes.position() > 0) {
  65.                             bytes.flip();
  66.                             buffer.append(UTF8.decode(bytes));
  67.                             bytes.clear();
  68.                         }
  69.                     }
  70.                 }
  71.                 buffer.append(url.charAt(i++));
  72.             }
  73.             decoded = buffer.toString();
  74.         }
  75.         return decoded;
  76.     }

  77.     /**
  78.      * Convert from a {@code URL} to a {@code File}.
  79.      * <p>
  80.      * From version 1.1 this method will decode the URL. Syntax such as {@code file:///my%20docs/file.txt} will be correctly
  81.      * decoded to {@code /my docs/file.txt}. Starting with version 1.5, this method uses UTF-8 to decode percent-encoded
  82.      * octets to characters. Additionally, malformed percent-encoded octets are handled leniently by passing them through
  83.      * literally.
  84.      *
  85.      * @param url the file URL to convert, {@code null} returns {@code null}
  86.      * @return the equivalent {@code File} object, or {@code null} if the URL's protocol is not {@code file}
  87.      */
  88.     public static File toFile(final URL url) {
  89.         if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) {
  90.             return null;
  91.         }
  92.         String fileName = url.getFile().replace('/', File.separatorChar);
  93.         fileName = decodeUrl(fileName);
  94.         return new File(fileName);
  95.     }

  96. }