forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CollectionUtil and IO util classes in sentinel-core
- Loading branch information
1 parent
cb21446
commit a70e139
Showing
5 changed files
with
726 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/CollectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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 | ||
* | ||
* https://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.alibaba.csp.sentinel.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Eric Zhao | ||
* @author lwj | ||
* @since 2.0.0 | ||
*/ | ||
public final class CollectionUtil { | ||
|
||
private CollectionUtil() {} | ||
|
||
public static boolean isEmpty(Collection<?> c) { | ||
return c == null || c.isEmpty(); | ||
} | ||
|
||
public static boolean isEmpty(Map<?, ?> c) { | ||
return c == null || c.isEmpty(); | ||
} | ||
} |
178 changes: 178 additions & 0 deletions
178
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/io/Charsets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* 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.alibaba.csp.sentinel.util.io; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* Copy from apache commons-io. | ||
*/ | ||
public final class Charsets { | ||
|
||
/** | ||
* 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 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 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 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 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 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 StandardCharsets} | ||
*/ | ||
@Deprecated | ||
public static final Charset UTF_8 = StandardCharsets.UTF_8; | ||
|
||
private Charsets() { | ||
} | ||
|
||
/** | ||
* 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() | ||
*/ | ||
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); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/io/FileUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.alibaba.csp.sentinel.util.io; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* Copy from apache commons-io. | ||
*/ | ||
public final class FileUtil { | ||
|
||
private FileUtil() { | ||
} | ||
|
||
// ----------------------------------------------------------------------- | ||
|
||
/** | ||
* Opens a {@link FileInputStream} for the specified file, providing better | ||
* error messages than simply calling <code>new FileInputStream(file)</code>. | ||
* <p> | ||
* At the end of the method either the stream will be successfully opened, or an | ||
* exception will have been thrown. | ||
* <p> | ||
* An exception is thrown if the file does not exist. An exception is thrown if the | ||
* file object exists but is a directory. An exception is thrown if the file exists | ||
* but cannot be read. | ||
* | ||
* @param file the file to open for input, must not be {@code null} | ||
* @return a new {@link FileInputStream} for the specified file | ||
* @throws FileNotFoundException if the file does not exist | ||
* @throws IOException if the file object is a directory | ||
* @throws IOException if the file cannot be read | ||
* @since 1.3 | ||
*/ | ||
public static FileInputStream openInputStream(final File file) throws IOException { | ||
if (file.exists()) { | ||
if (file.isDirectory()) { | ||
throw new IOException("File '" + file + "' exists but is a directory"); | ||
} | ||
if (!file.canRead()) { | ||
throw new IOException("File '" + file + "' cannot be read"); | ||
} | ||
} else { | ||
throw new FileNotFoundException("File '" + file + "' does not exist"); | ||
} | ||
return new FileInputStream(file); | ||
} | ||
|
||
// ----------------------------------------------------------------------- | ||
|
||
/** | ||
* Reads the contents of a file into a String. The file is always closed. | ||
* | ||
* @param file the file to read, must not be {@code null} | ||
* @param encoding the encoding to use, {@code null} means platform default | ||
* @return the file contents, never {@code null} | ||
* @throws IOException in case of an I/O error | ||
*/ | ||
public static String readFileToString(final File file, final Charset encoding) | ||
throws IOException { | ||
try (InputStream in = openInputStream(file)) { | ||
return IOUtil.toString(in, Charsets.toCharset(encoding)); | ||
} | ||
} | ||
|
||
/** | ||
* Reads the contents of a file into a String. The file is always closed. | ||
* | ||
* @param file the file to read, must not be {@code null} | ||
* @param encoding the encoding to use, {@code null} means platform default | ||
* @return the file contents, never {@code null} | ||
* @throws IOException in case of an I/O error | ||
* @throws java.nio.charset.UnsupportedCharsetException thrown instead of | ||
* {@link java.io .UnsupportedEncodingException} in version 2.2 if the encoding is not | ||
* supported. | ||
*/ | ||
public static String readFileToString(final File file, final String encoding) | ||
throws IOException { | ||
return readFileToString(file, Charsets.toCharset(encoding)); | ||
} | ||
|
||
/** | ||
* Reads the contents of a file into a String using the default encoding for the VM. | ||
* The file is always closed. | ||
* | ||
* @param file the file to read, must not be {@code null} | ||
* @return the file contents, never {@code null} | ||
* @throws IOException in case of an I/O error | ||
* @deprecated 2.5 use {@link #readFileToString(File, String)} instead (and specify | ||
* the appropriate encoding) | ||
*/ | ||
@Deprecated | ||
public static String readFileToString(final File file) throws IOException { | ||
return readFileToString(file, Charset.defaultCharset()); | ||
} | ||
|
||
} |
Oops, something went wrong.