|
| 1 | +/* |
| 2 | + * @(#)AgentUtils.java 2015-7-27 下午05:26:24 |
| 3 | + * javaagent |
| 4 | + * Copyright 2015 wenshuo, Inc. All rights reserved. |
| 5 | + * wenshuo PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
| 6 | + */ |
| 7 | +package com.wenshuo.agent; |
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.io.OutputStream; |
| 14 | +import java.io.Writer; |
| 15 | + |
| 16 | +/** |
| 17 | + * AgentUtils |
| 18 | + * |
| 19 | + * @author dingjsh |
| 20 | + * @time 2015-7-27下午05:26:24 |
| 21 | + */ |
| 22 | +public class AgentUtils { |
| 23 | + |
| 24 | + /** |
| 25 | + * <p> |
| 26 | + * Checks if a String is whitespace, empty ("") or null. |
| 27 | + * </p> |
| 28 | + * |
| 29 | + * <pre> |
| 30 | + * StringUtils.isBlank(null) = true |
| 31 | + * StringUtils.isBlank("") = true |
| 32 | + * StringUtils.isBlank(" ") = true |
| 33 | + * StringUtils.isBlank("bob") = false |
| 34 | + * StringUtils.isBlank(" bob ") = false |
| 35 | + * </pre> |
| 36 | + * |
| 37 | + * @param str the String to check, may be null |
| 38 | + * @return <code>true</code> if the String is null, empty or whitespace |
| 39 | + * @since 2.0 |
| 40 | + */ |
| 41 | + public static boolean isBlank(String str) { |
| 42 | + int strLen; |
| 43 | + if (str == null || (strLen = str.length()) == 0) { |
| 44 | + return true; |
| 45 | + } |
| 46 | + for (int i = 0; i < strLen; i++) { |
| 47 | + if (!Character.isWhitespace(str.charAt(i))) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * <p> |
| 56 | + * Checks if a String is not empty (""), not null and not whitespace only. |
| 57 | + * </p> |
| 58 | + * |
| 59 | + * <pre> |
| 60 | + * StringUtils.isNotBlank(null) = false |
| 61 | + * StringUtils.isNotBlank("") = false |
| 62 | + * StringUtils.isNotBlank(" ") = false |
| 63 | + * StringUtils.isNotBlank("bob") = true |
| 64 | + * StringUtils.isNotBlank(" bob ") = true |
| 65 | + * </pre> |
| 66 | + * |
| 67 | + * @param str the String to check, may be null |
| 68 | + * @return <code>true</code> if the String is not empty and not null and not |
| 69 | + * whitespace |
| 70 | + * @since 2.0 |
| 71 | + */ |
| 72 | + static boolean isNotBlank(String str) { |
| 73 | + return !isBlank(str); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Unconditionally close an <code>InputStream</code>. |
| 78 | + * <p> |
| 79 | + * Equivalent to {@link InputStream#close()}, except any exceptions will be |
| 80 | + * ignored. This is typically used in finally blocks. |
| 81 | + * |
| 82 | + * @param input the InputStream to close, may be null or already closed |
| 83 | + */ |
| 84 | + static void closeQuietly(InputStream input) { |
| 85 | + try { |
| 86 | + if (input != null) { |
| 87 | + input.close(); |
| 88 | + } |
| 89 | + } catch (IOException ioe) { |
| 90 | + // ignore |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static int copy(InputStream input, OutputStream output) |
| 95 | + throws IOException { |
| 96 | + long count = copyLarge(input, output); |
| 97 | + if (count > Integer.MAX_VALUE) { |
| 98 | + return -1; |
| 99 | + } |
| 100 | + return (int) count; |
| 101 | + } |
| 102 | + |
| 103 | + private static long copyLarge(InputStream input, OutputStream output) |
| 104 | + throws IOException { |
| 105 | + byte[] buffer = new byte[4096]; |
| 106 | + long count = 0; |
| 107 | + int n; |
| 108 | + while (-1 != (n = input.read(buffer))) { |
| 109 | + output.write(buffer, 0, n); |
| 110 | + count += n; |
| 111 | + } |
| 112 | + return count; |
| 113 | + } |
| 114 | + |
| 115 | + // read toByteArray |
| 116 | + //----------------------------------------------------------------------- |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. |
| 120 | + * <p> |
| 121 | + * This method buffers the input internally, so there is no need to use a |
| 122 | + * <code>BufferedInputStream</code>. |
| 123 | + * |
| 124 | + * @param input the <code>InputStream</code> to read from |
| 125 | + * @return the requested byte array |
| 126 | + * @throws NullPointerException if the input is null |
| 127 | + * @throws IOException if an I/O error occurs |
| 128 | + */ |
| 129 | + public static byte[] toByteArray(InputStream input) throws IOException { |
| 130 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 131 | + copy(input, output); |
| 132 | + return output.toByteArray(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Unconditionally close a <code>Writer</code>. |
| 137 | + * <p> |
| 138 | + * Equivalent to {@link Writer#close()}, except any exceptions will be ignored. |
| 139 | + * This is typically used in finally blocks. |
| 140 | + * |
| 141 | + * @param output the Writer to close, may be null or already closed |
| 142 | + */ |
| 143 | + public static void closeQuietly(Writer output) { |
| 144 | + try { |
| 145 | + if (output != null) { |
| 146 | + output.close(); |
| 147 | + } |
| 148 | + } catch (IOException ioe) { |
| 149 | + // ignore |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Makes a directory, including any necessary but nonexistent parent |
| 155 | + * directories. If there already exists a file with specified name or |
| 156 | + * the directory cannot be created then an exception is thrown. |
| 157 | + * |
| 158 | + * @param directory directory to create, must not be <code>null</code> |
| 159 | + * @throws NullPointerException if the directory is <code>null</code> |
| 160 | + * @throws IOException if the directory cannot be created |
| 161 | + */ |
| 162 | + public static void forceMkdir(File directory) throws IOException { |
| 163 | + if (directory.exists()) { |
| 164 | + if (directory.isFile()) { |
| 165 | + String message = |
| 166 | + "File " |
| 167 | + + directory |
| 168 | + + " exists and is " |
| 169 | + + "not a directory. Unable to create directory."; |
| 170 | + throw new IOException(message); |
| 171 | + } |
| 172 | + } else { |
| 173 | + if (!directory.mkdirs()) { |
| 174 | + String message = |
| 175 | + "Unable to create directory " + directory; |
| 176 | + throw new IOException(message); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments