richie
7 years ago
122 changed files with 1517 additions and 2095 deletions
@ -1,66 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
|
||||
package com.fr.third.org.apache.commons.io; |
||||
|
||||
import java.nio.ByteOrder; |
||||
|
||||
/** |
||||
* Converts Strings to {@link ByteOrder} instances. |
||||
* |
||||
* @since 2.6 |
||||
*/ |
||||
public final class ByteOrderParser { |
||||
|
||||
/** |
||||
* ByteOrderUtils is a static utility class, so prevent construction with a private constructor. |
||||
*/ |
||||
private ByteOrderParser() { |
||||
} |
||||
|
||||
/** |
||||
* Parses the String argument as a {@link ByteOrder}. |
||||
* <p> |
||||
* Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "LITTLE_ENDIAN"}. |
||||
* </p> |
||||
* <p> |
||||
* Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "BIG_ENDIAN"}. |
||||
* </p> |
||||
* Examples: |
||||
* <ul> |
||||
* <li>{@code ByteOrderParser.parseByteOrder("LITTLE_ENDIAN")} returns {@code ByteOrder.LITTLE_ENDIAN}</li> |
||||
* <li>{@code ByteOrderParser.parseByteOrder("BIG_ENDIAN")} returns {@code ByteOrder.BIG_ENDIAN}</li> |
||||
* </ul> |
||||
* |
||||
* @param value |
||||
* the {@code String} containing the ByteOrder representation to be parsed |
||||
* @return the ByteOrder represented by the string argument |
||||
* @throws IllegalArgumentException |
||||
* if the {@code String} containing the ByteOrder representation to be parsed is unknown. |
||||
*/ |
||||
public static ByteOrder parseByteOrder(final String value) { |
||||
if (ByteOrder.BIG_ENDIAN.toString().equals(value)) { |
||||
return ByteOrder.BIG_ENDIAN; |
||||
} |
||||
if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) { |
||||
return ByteOrder.LITTLE_ENDIAN; |
||||
} |
||||
throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN + |
||||
", " + ByteOrder.BIG_ENDIAN); |
||||
} |
||||
|
||||
} |
@ -1,192 +1,192 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
package com.fr.third.org.apache.commons.io; |
||||
|
||||
import java.nio.charset.Charset; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Collections; |
||||
import java.util.SortedMap; |
||||
import java.util.TreeMap; |
||||
|
||||
/** |
||||
* Charsets required of every implementation of the Java platform. |
||||
* |
||||
* From the Java documentation <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html"> |
||||
* Standard charsets</a>: |
||||
* <p> |
||||
* <cite>Every implementation of the Java platform is required to support the following character encodings. Consult |
||||
* the release documentation for your implementation to see if any other encodings are supported. Consult the release |
||||
* documentation for your implementation to see if any other encodings are supported. </cite> |
||||
* </p> |
||||
* |
||||
* <ul> |
||||
* <li><code>US-ASCII</code><br> |
||||
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li> |
||||
* <li><code>ISO-8859-1</code><br> |
||||
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li> |
||||
* <li><code>UTF-8</code><br> |
||||
* Eight-bit Unicode Transformation Format.</li> |
||||
* <li><code>UTF-16BE</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, big-endian byte order.</li> |
||||
* <li><code>UTF-16LE</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, little-endian byte order.</li> |
||||
* <li><code>UTF-16</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order |
||||
* accepted on input, big-endian used on output.)</li> |
||||
* </ul> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @since 2.3 |
||||
* @version $Id$ |
||||
*/ |
||||
public class Charsets { |
||||
//
|
||||
// This class should only contain Charset instances for required encodings. This guarantees that it will load
|
||||
// correctly and without delay on all Java platforms.
|
||||
//
|
||||
|
||||
/** |
||||
* Constructs a sorted map from canonical charset names to charset objects required of every implementation of the |
||||
* Java platform. |
||||
* <p> |
||||
* From the Java documentation <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html"> |
||||
* Standard charsets</a>: |
||||
* </p> |
||||
* |
||||
* @return An immutable, case-insensitive map from canonical charset names to charset objects. |
||||
* @see Charset#availableCharsets() |
||||
* @since 2.5 |
||||
*/ |
||||
public static SortedMap<String, Charset> requiredCharsets() { |
||||
// maybe cache?
|
||||
final TreeMap<String, Charset> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
||||
m.put(StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1); |
||||
m.put(StandardCharsets.US_ASCII.name(), StandardCharsets.US_ASCII); |
||||
m.put(StandardCharsets.UTF_16.name(), StandardCharsets.UTF_16); |
||||
m.put(StandardCharsets.UTF_16BE.name(), StandardCharsets.UTF_16BE); |
||||
m.put(StandardCharsets.UTF_16LE.name(), StandardCharsets.UTF_16LE); |
||||
m.put(StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8); |
||||
return Collections.unmodifiableSortedMap(m); |
||||
} |
||||
|
||||
/** |
||||
* Returns the given Charset or the default Charset if the given Charset is null. |
||||
* |
||||
* @param charset |
||||
* A charset or null. |
||||
* @return the given Charset or the default Charset if the given Charset is null |
||||
*/ |
||||
public static Charset toCharset(final Charset charset) { |
||||
return charset == null ? Charset.defaultCharset() : charset; |
||||
} |
||||
|
||||
/** |
||||
* Returns a Charset for the named charset. If the name is null, return the default Charset. |
||||
* |
||||
* @param charset |
||||
* The name of the requested charset, may be null. |
||||
* @return a Charset for the named charset |
||||
* @throws java.nio.charset.UnsupportedCharsetException |
||||
* If the named charset is unavailable |
||||
*/ |
||||
public static Charset toCharset(final String charset) { |
||||
return charset == null ? Charset.defaultCharset() : Charset.forName(charset); |
||||
} |
||||
|
||||
/** |
||||
* CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; |
||||
|
||||
/** |
||||
* <p> |
||||
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset US_ASCII = StandardCharsets.US_ASCII; |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark |
||||
* (either order accepted on input, big-endian used on output) |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16 = StandardCharsets.UTF_16; |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, big-endian byte order. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16BE = StandardCharsets.UTF_16BE; |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, little-endian byte order. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16LE = StandardCharsets.UTF_16LE; |
||||
|
||||
/** |
||||
* <p> |
||||
* Eight-bit Unicode Transformation Format. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_8 = StandardCharsets.UTF_8; |
||||
} |
||||
/* |
||||
* 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. |
||||
*/ |
||||
package com.fr.third.org.apache.commons.io; |
||||
|
||||
import java.nio.charset.Charset; |
||||
import java.util.Collections; |
||||
import java.util.SortedMap; |
||||
import java.util.TreeMap; |
||||
|
||||
/** |
||||
* Charsets required of every implementation of the Java platform. |
||||
* |
||||
* From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html"> |
||||
* Standard charsets</a>: |
||||
* <p> |
||||
* <cite>Every implementation of the Java platform is required to support the following character encodings. Consult |
||||
* the release documentation for your implementation to see if any other encodings are supported. Consult the release |
||||
* documentation for your implementation to see if any other encodings are supported. </cite> |
||||
* </p> |
||||
* |
||||
* <ul> |
||||
* <li><code>US-ASCII</code><br> |
||||
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li> |
||||
* <li><code>ISO-8859-1</code><br> |
||||
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li> |
||||
* <li><code>UTF-8</code><br> |
||||
* Eight-bit Unicode Transformation Format.</li> |
||||
* <li><code>UTF-16BE</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, big-endian byte order.</li> |
||||
* <li><code>UTF-16LE</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, little-endian byte order.</li> |
||||
* <li><code>UTF-16</code><br> |
||||
* Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order |
||||
* accepted on input, big-endian used on output.)</li> |
||||
* </ul> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @since 2.3 |
||||
* @version $Id: Charsets.java 1686747 2015-06-21 18:44:49Z krosenvold $ |
||||
*/ |
||||
public class Charsets { |
||||
//
|
||||
// This class should only contain Charset instances for required encodings. This guarantees that it will load
|
||||
// correctly and without delay on all Java platforms.
|
||||
//
|
||||
|
||||
/** |
||||
* Constructs a sorted map from canonical charset names to charset objects required of every implementation of the |
||||
* Java platform. |
||||
* <p> |
||||
* From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html"> |
||||
* Standard charsets</a>: |
||||
* </p> |
||||
* |
||||
* @return An immutable, case-insensitive map from canonical charset names to charset objects. |
||||
* @see Charset#availableCharsets() |
||||
* @since 2.5 |
||||
*/ |
||||
public static SortedMap<String, Charset> requiredCharsets() { |
||||
// maybe cache?
|
||||
// TODO Re-implement on Java 7 to use java.nio.charset.StandardCharsets
|
||||
final TreeMap<String, Charset> m = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER); |
||||
m.put(ISO_8859_1.name(), ISO_8859_1); |
||||
m.put(US_ASCII.name(), US_ASCII); |
||||
m.put(UTF_16.name(), UTF_16); |
||||
m.put(UTF_16BE.name(), UTF_16BE); |
||||
m.put(UTF_16LE.name(), UTF_16LE); |
||||
m.put(UTF_8.name(), UTF_8); |
||||
return Collections.unmodifiableSortedMap(m); |
||||
} |
||||
|
||||
/** |
||||
* Returns the given Charset or the default Charset if the given Charset is null. |
||||
* |
||||
* @param charset |
||||
* A charset or null. |
||||
* @return the given Charset or the default Charset if the given Charset is null |
||||
*/ |
||||
public static Charset toCharset(final Charset charset) { |
||||
return charset == null ? Charset.defaultCharset() : charset; |
||||
} |
||||
|
||||
/** |
||||
* Returns a Charset for the named charset. If the name is null, return the default Charset. |
||||
* |
||||
* @param charset |
||||
* The name of the requested charset, may be null. |
||||
* @return a Charset for the named charset |
||||
* @throws java.nio.charset.UnsupportedCharsetException |
||||
* If the named charset is unavailable |
||||
*/ |
||||
public static Charset toCharset(final String charset) { |
||||
return charset == null ? Charset.defaultCharset() : Charset.forName(charset); |
||||
} |
||||
|
||||
/** |
||||
* CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); |
||||
|
||||
/** |
||||
* <p> |
||||
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset US_ASCII = Charset.forName("US-ASCII"); |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark |
||||
* (either order accepted on input, big-endian used on output) |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16 = Charset.forName("UTF-16"); |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, big-endian byte order. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16BE = Charset.forName("UTF-16BE"); |
||||
|
||||
/** |
||||
* <p> |
||||
* Sixteen-bit Unicode Transformation Format, little-endian byte order. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_16LE = Charset.forName("UTF-16LE"); |
||||
|
||||
/** |
||||
* <p> |
||||
* Eight-bit Unicode Transformation Format. |
||||
* </p> |
||||
* <p> |
||||
* Every implementation of the Java platform is required to support this character encoding. |
||||
* </p> |
||||
* |
||||
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
||||
* @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets} |
||||
*/ |
||||
@Deprecated |
||||
public static final Charset UTF_8 = Charset.forName("UTF-8"); |
||||
} |
||||
|
@ -0,0 +1,190 @@
|
||||
package com.fr.third.org.apache.commons.io; |
||||
|
||||
/* |
||||
* 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.File; |
||||
import java.io.IOException; |
||||
import java.lang.reflect.Array; |
||||
import java.lang.reflect.InvocationTargetException; |
||||
import java.lang.reflect.Method; |
||||
|
||||
/** |
||||
* Java7 feature detection and reflection based feature access. |
||||
* <p/> |
||||
* Taken from maven-shared-utils, only for private usage until we go full java7 |
||||
*/ |
||||
class Java7Support { |
||||
|
||||
private static final boolean IS_JAVA7; |
||||
|
||||
private static Method isSymbolicLink; |
||||
|
||||
private static Method delete; |
||||
|
||||
private static Method toPath; |
||||
|
||||
private static Method exists; |
||||
|
||||
private static Method toFile; |
||||
|
||||
private static Method readSymlink; |
||||
|
||||
private static Method createSymlink; |
||||
|
||||
private static Object emptyLinkOpts; |
||||
|
||||
private static Object emptyFileAttributes; |
||||
|
||||
static { |
||||
boolean isJava7x = true; |
||||
try { |
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
||||
Class<?> files = cl.loadClass("java.nio.file.Files"); |
||||
Class<?> path = cl.loadClass("java.nio.file.Path"); |
||||
Class<?> fa = cl.loadClass("java.nio.file.attribute.FileAttribute"); |
||||
Class<?> linkOption = cl.loadClass("java.nio.file.LinkOption"); |
||||
isSymbolicLink = files.getMethod("isSymbolicLink", path); |
||||
delete = files.getMethod("delete", path); |
||||
readSymlink = files.getMethod("readSymbolicLink", path); |
||||
|
||||
emptyFileAttributes = Array.newInstance(fa, 0); |
||||
createSymlink = files.getMethod("createSymbolicLink", path, path, emptyFileAttributes.getClass()); |
||||
emptyLinkOpts = Array.newInstance(linkOption, 0); |
||||
exists = files.getMethod("exists", path, emptyLinkOpts.getClass()); |
||||
toPath = File.class.getMethod("toPath"); |
||||
toFile = path.getMethod("toFile"); |
||||
} catch (ClassNotFoundException e) { |
||||
isJava7x = false; |
||||
} catch (NoSuchMethodException e) { |
||||
isJava7x = false; |
||||
} |
||||
IS_JAVA7 = isJava7x; |
||||
} |
||||
|
||||
/** |
||||
* Invokes java7 isSymbolicLink |
||||
* @param file The file to check |
||||
* @return true if the file is a symbolic link |
||||
*/ |
||||
public static boolean isSymLink(File file) { |
||||
try { |
||||
Object path = toPath.invoke(file); |
||||
Boolean result = (Boolean) isSymbolicLink.invoke(null, path); |
||||
return result.booleanValue(); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (InvocationTargetException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Reads the target of a symbolic link |
||||
* @param symlink The symlink to read |
||||
* @return The location the symlink is pointing to |
||||
* @throws IOException Upon failure |
||||
*/ |
||||
|
||||
public static File readSymbolicLink(File symlink) |
||||
throws IOException { |
||||
try { |
||||
Object path = toPath.invoke(symlink); |
||||
Object resultPath = readSymlink.invoke(null, path); |
||||
return (File) toFile.invoke(resultPath); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (InvocationTargetException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Indicates if a symlunk target exists |
||||
* @param file The symlink file |
||||
* @return true if the target exists |
||||
* @throws IOException upon error |
||||
*/ |
||||
private static boolean exists(File file) |
||||
throws IOException { |
||||
try { |
||||
Object path = toPath.invoke(file); |
||||
final Boolean result = (Boolean) exists.invoke(null, path, emptyLinkOpts); |
||||
return result.booleanValue(); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (InvocationTargetException e) { |
||||
throw (RuntimeException) e.getTargetException(); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Creates a symbolic link |
||||
* @param symlink The symlink to create |
||||
* @param target Where it should point |
||||
* @return The newly created symlink |
||||
* @throws IOException upon error |
||||
*/ |
||||
public static File createSymbolicLink(File symlink, File target) |
||||
throws IOException { |
||||
try { |
||||
if (!exists(symlink)) { |
||||
Object link = toPath.invoke(symlink); |
||||
Object path = createSymlink.invoke(null, link, toPath.invoke(target), emptyFileAttributes); |
||||
return (File) toFile.invoke(path); |
||||
} |
||||
return symlink; |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (InvocationTargetException e) { |
||||
final Throwable targetException = e.getTargetException(); |
||||
throw (IOException) targetException; |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Performs a nio delete |
||||
* |
||||
* @param file the file to delete |
||||
* @throws IOException Upon error |
||||
*/ |
||||
public static void delete(File file) |
||||
throws IOException { |
||||
try { |
||||
Object path = toPath.invoke(file); |
||||
delete.invoke(null, path); |
||||
} catch (IllegalAccessException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (InvocationTargetException e) { |
||||
throw (IOException) e.getTargetException(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Indicates if the current vm has java7 lubrary support |
||||
* @return true if java7 library support |
||||
*/ |
||||
public static boolean isAtLeastJava7() { |
||||
return IS_JAVA7; |
||||
} |
||||
|
||||
} |
@ -1,52 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
package com.fr.third.org.apache.commons.io.input; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* |
||||
* An {@link InputStream} that infinitely repeats provided bytes. |
||||
* <p> |
||||
* Closing a <tt>InfiniteCircularInputStream</tt> has no effect. The methods in |
||||
* this class can be called after the stream has been closed without generating |
||||
* an <tt>IOException</tt>. |
||||
* |
||||
*/ |
||||
public class InfiniteCircularInputStream extends InputStream { |
||||
|
||||
final private byte[] repeatedContent; |
||||
private int position = -1; |
||||
|
||||
/** |
||||
* Creates a InfiniteCircularStream from the specified array of chars. |
||||
* |
||||
* @param repeatedContent |
||||
* Input buffer to be repeated (not copied) |
||||
*/ |
||||
public InfiniteCircularInputStream(final byte[] repeatedContent) { |
||||
this.repeatedContent = repeatedContent; |
||||
} |
||||
|
||||
@Override |
||||
public int read() { |
||||
position = (position + 1) % repeatedContent.length; |
||||
return repeatedContent[position] & 0xff; // copied from
|
||||
// java.io.ByteArrayInputStream.read()
|
||||
} |
||||
|
||||
} |
@ -1,102 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
package com.fr.third.org.apache.commons.io.input; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
|
||||
/** |
||||
* This class is an example for using an {@link ObservableInputStream}. It |
||||
* creates its own {@link Observer}, |
||||
* which calculates a checksum using a MessageDigest, for example an MD5 sum. |
||||
* <em>Note</em>: Neither {@link ObservableInputStream}, nor {@link MessageDigest}, |
||||
* are thread safe. So is {@link MessageDigestCalculatingInputStream}. |
||||
*/ |
||||
public class MessageDigestCalculatingInputStream extends ObservableInputStream { |
||||
|
||||
/** |
||||
* Maintains the message digest. |
||||
*/ |
||||
public static class MessageDigestMaintainingObserver extends Observer { |
||||
private final MessageDigest md; |
||||
|
||||
/** |
||||
* Creates an MessageDigestMaintainingObserver for the given MessageDigest. |
||||
* @param pMd the message digest to use |
||||
*/ |
||||
public MessageDigestMaintainingObserver(final MessageDigest pMd) { |
||||
md = pMd; |
||||
} |
||||
|
||||
@Override |
||||
void data(final int pByte) throws IOException { |
||||
md.update((byte) pByte); |
||||
} |
||||
|
||||
@Override |
||||
void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { |
||||
md.update(pBuffer, pOffset, pLength); |
||||
} |
||||
} |
||||
|
||||
private final MessageDigest messageDigest; |
||||
|
||||
/** Creates a new instance, which calculates a signature on the given stream, |
||||
* using the given {@link MessageDigest}. |
||||
* @param pStream the stream to calculate the message digest for |
||||
* @param pDigest the message digest to use |
||||
*/ |
||||
public MessageDigestCalculatingInputStream(final InputStream pStream, final MessageDigest pDigest) { |
||||
super(pStream); |
||||
messageDigest = pDigest; |
||||
add(new MessageDigestMaintainingObserver(pDigest)); |
||||
} |
||||
|
||||
/** Creates a new instance, which calculates a signature on the given stream, |
||||
* using a {@link MessageDigest} with the given algorithm. |
||||
* @param pStream the stream to calculate the message digest for |
||||
* @param pAlgorithm the name of the algorithm to use |
||||
* @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm. |
||||
*/ |
||||
public MessageDigestCalculatingInputStream(final InputStream pStream, final String pAlgorithm) throws NoSuchAlgorithmException { |
||||
this(pStream, MessageDigest.getInstance(pAlgorithm)); |
||||
} |
||||
|
||||
/** Creates a new instance, which calculates a signature on the given stream, |
||||
* using a {@link MessageDigest} with the "MD5" algorithm. |
||||
* @param pStream the stream to calculate the message digest for |
||||
* @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm. |
||||
*/ |
||||
public MessageDigestCalculatingInputStream(final InputStream pStream) throws NoSuchAlgorithmException { |
||||
this(pStream, MessageDigest.getInstance("MD5")); |
||||
} |
||||
|
||||
/** Returns the {@link MessageDigest}, which is being used for generating the |
||||
* checksum. |
||||
* <em>Note</em>: The checksum will only reflect the data, which has been read so far. |
||||
* This is probably not, what you expect. Make sure, that the complete data has been |
||||
* read, if that is what you want. The easiest way to do so is by invoking |
||||
* {@link #consume()}. |
||||
* @return the message digest used |
||||
*/ |
||||
public MessageDigest getMessageDigest() { |
||||
return messageDigest; |
||||
} |
||||
} |
@ -1,266 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
package com.fr.third.org.apache.commons.io.input; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
|
||||
/** |
||||
* The {@link ObservableInputStream} allows, that an InputStream may be consumed |
||||
* by other receivers, apart from the thread, which is reading it. |
||||
* The other consumers are implemented as instances of {@link Observer}. A |
||||
* typical application may be the generation of a {@link java.security.MessageDigest} on the |
||||
* fly. |
||||
* {@code Note}: The {@link ObservableInputStream} is <em>not</em> thread safe, |
||||
* as instances of InputStream usually aren't. |
||||
* If you must access the stream from multiple threads, then synchronization, locking, |
||||
* or a similar means must be used. |
||||
* @see MessageDigestCalculatingInputStream |
||||
*/ |
||||
public class ObservableInputStream extends ProxyInputStream { |
||||
|
||||
public static abstract class Observer { |
||||
|
||||
/** Called to indicate, that {@link InputStream#read()} has been invoked |
||||
* on the {@link ObservableInputStream}, and will return a value. |
||||
* @param pByte The value, which is being returned. This will never be -1 (EOF), |
||||
* because, in that case, {@link #finished()} will be invoked instead. |
||||
* @throws IOException if an i/o-error occurs |
||||
*/ |
||||
void data(final int pByte) throws IOException {} |
||||
|
||||
/** Called to indicate, that {@link InputStream#read(byte[])}, or |
||||
* {@link InputStream#read(byte[], int, int)} have been called, and are about to |
||||
* invoke data. |
||||
* @param pBuffer The byte array, which has been passed to the read call, and where |
||||
* data has been stored. |
||||
* @param pOffset The offset within the byte array, where data has been stored. |
||||
* @param pLength The number of bytes, which have been stored in the byte array. |
||||
* @throws IOException if an i/o-error occurs |
||||
*/ |
||||
void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {} |
||||
|
||||
/** Called to indicate, that EOF has been seen on the underlying stream. |
||||
* This method may be called multiple times, if the reader keeps invoking |
||||
* either of the read methods, and they will consequently keep returning |
||||
* EOF. |
||||
* @throws IOException if an i/o-error occurs |
||||
*/ |
||||
void finished() throws IOException {} |
||||
|
||||
/** Called to indicate, that the {@link ObservableInputStream} has been closed. |
||||
* @throws IOException if an i/o-error occurs |
||||
*/ |
||||
void closed() throws IOException {} |
||||
|
||||
/** |
||||
* Called to indicate, that an error occurred on the underlying stream. |
||||
* @throws IOException if an i/o-error occurs |
||||
*/ |
||||
void error(final IOException pException) throws IOException { throw pException; } |
||||
} |
||||
|
||||
private final List<Observer> observers = new ArrayList<>(); |
||||
|
||||
/** |
||||
* Creates a new ObservableInputStream for the given InputStream. |
||||
* @param pProxy the input stream to proxy |
||||
*/ |
||||
public ObservableInputStream(final InputStream pProxy) { |
||||
super(pProxy); |
||||
} |
||||
|
||||
/** |
||||
* Adds an Observer. |
||||
* @param pObserver the observer to add |
||||
*/ |
||||
public void add(final Observer pObserver) { |
||||
observers.add(pObserver); |
||||
} |
||||
|
||||
/** |
||||
* Removes an Observer. |
||||
* @param pObserver the observer to remove |
||||
*/ |
||||
public void remove(final Observer pObserver) { |
||||
observers.remove(pObserver); |
||||
} |
||||
|
||||
/** |
||||
* Removes all Observers. |
||||
*/ |
||||
public void removeAllObservers() { |
||||
observers.clear(); |
||||
} |
||||
|
||||
@Override |
||||
public int read() throws IOException { |
||||
int result = 0; |
||||
IOException ioe = null; |
||||
try { |
||||
result = super.read(); |
||||
} catch (final IOException pException) { |
||||
ioe = pException; |
||||
} |
||||
if (ioe != null) { |
||||
noteError(ioe); |
||||
} else if (result == -1) { |
||||
noteFinished(); |
||||
} else { |
||||
noteDataByte(result); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public int read(final byte[] pBuffer) throws IOException { |
||||
int result = 0; |
||||
IOException ioe = null; |
||||
try { |
||||
result = super.read(pBuffer); |
||||
} catch (final IOException pException) { |
||||
ioe = pException; |
||||
} |
||||
if (ioe != null) { |
||||
noteError(ioe); |
||||
} else if (result == -1) { |
||||
noteFinished(); |
||||
} else if (result > 0) { |
||||
noteDataBytes(pBuffer, 0, result); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { |
||||
int result = 0; |
||||
IOException ioe = null; |
||||
try { |
||||
result = super.read(pBuffer, pOffset, pLength); |
||||
} catch (final IOException pException) { |
||||
ioe = pException; |
||||
} |
||||
if (ioe != null) { |
||||
noteError(ioe); |
||||
} else if (result == -1) { |
||||
noteFinished(); |
||||
} else if (result > 0) { |
||||
noteDataBytes(pBuffer, pOffset, result); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** Notifies the observers by invoking {@link Observer#data(byte[],int,int)} |
||||
* with the given arguments. |
||||
* @param pBuffer Passed to the observers. |
||||
* @param pOffset Passed to the observers. |
||||
* @param pLength Passed to the observers. |
||||
* @throws IOException Some observer has thrown an exception, which is being |
||||
* passed down. |
||||
*/ |
||||
protected void noteDataBytes(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { |
||||
for (final Observer observer : getObservers()) { |
||||
observer.data(pBuffer, pOffset, pLength); |
||||
} |
||||
} |
||||
|
||||
/** Notifies the observers by invoking {@link Observer#finished()}. |
||||
* @throws IOException Some observer has thrown an exception, which is being |
||||
* passed down. |
||||
*/ |
||||
protected void noteFinished() throws IOException { |
||||
for (final Observer observer : getObservers()) { |
||||
observer.finished(); |
||||
} |
||||
} |
||||
|
||||
/** Notifies the observers by invoking {@link Observer#data(int)} |
||||
* with the given arguments. |
||||
* @param pDataByte Passed to the observers. |
||||
* @throws IOException Some observer has thrown an exception, which is being |
||||
* passed down. |
||||
*/ |
||||
protected void noteDataByte(final int pDataByte) throws IOException { |
||||
for (final Observer observer : getObservers()) { |
||||
observer.data(pDataByte); |
||||
} |
||||
} |
||||
|
||||
/** Notifies the observers by invoking {@link Observer#error(IOException)} |
||||
* with the given argument. |
||||
* @param pException Passed to the observers. |
||||
* @throws IOException Some observer has thrown an exception, which is being |
||||
* passed down. This may be the same exception, which has been passed as an |
||||
* argument. |
||||
*/ |
||||
protected void noteError(final IOException pException) throws IOException { |
||||
for (final Observer observer : getObservers()) { |
||||
observer.error(pException); |
||||
} |
||||
} |
||||
|
||||
/** Notifies the observers by invoking {@link Observer#finished()}. |
||||
* @throws IOException Some observer has thrown an exception, which is being |
||||
* passed down. |
||||
*/ |
||||
protected void noteClosed() throws IOException { |
||||
for (final Observer observer : getObservers()) { |
||||
observer.closed(); |
||||
} |
||||
} |
||||
|
||||
/** Gets all currently registered observers. |
||||
* @return a list of the currently registered observers |
||||
*/ |
||||
protected List<Observer> getObservers() { |
||||
return observers; |
||||
} |
||||
|
||||
@Override |
||||
public void close() throws IOException { |
||||
IOException ioe = null; |
||||
try { |
||||
super.close(); |
||||
} catch (final IOException e) { |
||||
ioe = e; |
||||
} |
||||
if (ioe == null) { |
||||
noteClosed(); |
||||
} else { |
||||
noteError(ioe); |
||||
} |
||||
} |
||||
|
||||
/** Reads all data from the underlying {@link InputStream}, while notifying the |
||||
* observers. |
||||
* @throws IOException The underlying {@link InputStream}, or either of the |
||||
* observers has thrown an exception. |
||||
*/ |
||||
public void consume() throws IOException { |
||||
final byte[] buffer = new byte[8192]; |
||||
for (;;) { |
||||
final int res = read(buffer); |
||||
if (res == -1) { |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue