Changeset 7867 in josm for trunk/src/org/openstreetmap/josm/tools/Utils.java
- Timestamp:
- 2014-12-21T01:30:50+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r7864 r7867 51 51 import java.util.zip.ZipInputStream; 52 52 53 import org.apache. tools.bzip2.CBZip2InputStream;53 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; 54 54 import org.openstreetmap.josm.Main; 55 55 import org.openstreetmap.josm.data.Version; … … 159 159 160 160 /** 161 * Get minimum of 3 values 161 * Returns the minimum of three values. 162 * @param a an argument. 163 * @param b another argument. 164 * @param c another argument. 165 * @return the smaller of {@code a}, {@code b} and {@code c}. 162 166 */ 163 167 public static int min(int a, int b, int c) { … … 173 177 } 174 178 179 /** 180 * Returns the greater of four {@code int} values. That is, the 181 * result is the argument closer to the value of 182 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 183 * the result is that same value. 184 * 185 * @param a an argument. 186 * @param b another argument. 187 * @param c another argument. 188 * @param d another argument. 189 * @return the larger of {@code a}, {@code b}, {@code c} and {@code d}. 190 */ 175 191 public static int max(int a, int b, int c, int d) { 176 192 return Math.max(Math.max(a, b), Math.max(c, d)); 177 193 } 178 194 195 /** 196 * Ensures a logical condition is met. Otherwise throws an assertion error. 197 * @param condition the condition to be met 198 * @param message Formatted error message to raise if condition is not met 199 * @param data Message parameters, optional 200 * @throws AssertionError if the condition is not met 201 */ 179 202 public static void ensure(boolean condition, String message, Object...data) { 180 203 if (!condition) … … 209 232 if (values == null) 210 233 return null; 211 if (values.isEmpty())212 return "";213 234 StringBuilder s = null; 214 235 for (Object a : values) { … … 222 243 } 223 244 } 224 return s .toString();245 return s != null ? s.toString() : ""; 225 246 } 226 247 … … 361 382 } 362 383 384 /** 385 * Copy data from source stream to output stream. 386 * @param source source stream 387 * @param destination target stream 388 * @return number of bytes copied 389 * @throws IOException if any I/O error occurs 390 */ 363 391 public static int copyStream(InputStream source, OutputStream destination) throws IOException { 364 392 int count = 0; … … 702 730 * Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary. 703 731 * @param url The url to open 704 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream}732 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream} 705 733 * if the {@code Content-Type} header is set accordingly. 706 734 * @return An stream for the given URL … … 729 757 * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null} 730 758 * @throws IOException if the given input stream does not contain valid BZ2 header 731 * @since 7 119732 */ 733 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {759 * @since 7867 760 */ 761 public static BZip2CompressorInputStream getBZip2InputStream(InputStream in) throws IOException { 734 762 if (in == null) { 735 763 return null; 736 764 } 737 765 BufferedInputStream bis = new BufferedInputStream(in); 738 int b = bis.read(); 739 if (b != 'B') 740 throw new IOException(tr("Invalid bz2 file.")); 741 b = bis.read(); 742 if (b != 'Z') 743 throw new IOException(tr("Invalid bz2 file.")); 744 return new CBZip2InputStream(bis, /* see #9537 */ true); 766 return new BZip2CompressorInputStream(bis, /* see #9537 */ true); 745 767 } 746 768 … … 808 830 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one. 809 831 * @param url The url to open 810 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream}832 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream} 811 833 * if the {@code Content-Type} header is set accordingly. 812 834 * @return An buffered stream reader for the given URL (using UTF-8) … … 929 951 * @since 6354 930 952 */ 931 public static String getDurationString(long elapsedTime) throws IllegalArgumentException{953 public static String getDurationString(long elapsedTime) { 932 954 if (elapsedTime < 0) { 933 955 throw new IllegalArgumentException("elapsedTime must be >= 0"); … … 1033 1055 if (result != null) { 1034 1056 Throwable cause = result.getCause(); 1035 while (cause != null && cause != result) {1057 while (cause != null && !cause.equals(result)) { 1036 1058 result = cause; 1037 1059 cause = result.getCause(); … … 1056 1078 /** 1057 1079 * If the string {@code s} is longer than {@code maxLength}, the string is cut and "..." is appended. 1080 * @param s String to shorten 1081 * @param maxLength maximum number of characters to keep (not including the "...") 1082 * @return the shortened string 1058 1083 */ 1059 1084 public static String shortenString(String s, int maxLength) {
Note:
See TracChangeset
for help on using the changeset viewer.