source: josm/trunk/src/com/drew/lang/StringUtil.java@ 14826

Last change on this file since 14826 was 13061, checked in by Don-vip, 7 years ago

fix #15505 - update to metadata-extractor 2.10.1

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.3 KB
Line 
1/*
2 * Copyright 2002-2017 Drew Noakes
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21
22package com.drew.lang;
23
24import com.drew.lang.annotations.NotNull;
25import com.drew.lang.annotations.Nullable;
26
27import java.io.BufferedReader;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.util.Iterator;
32
33/**
34 * @author Drew Noakes https://drewnoakes.com
35 */
36public final class StringUtil
37{
38 @NotNull
39 public static String join(@NotNull Iterable<? extends CharSequence> strings, @NotNull String delimiter)
40 {
41 int capacity = 0;
42 int delimLength = delimiter.length();
43
44 Iterator<? extends CharSequence> iter = strings.iterator();
45 if (iter.hasNext())
46 capacity += iter.next().length() + delimLength;
47
48 StringBuilder buffer = new StringBuilder(capacity);
49 iter = strings.iterator();
50 if (iter.hasNext()) {
51 buffer.append(iter.next());
52 while (iter.hasNext()) {
53 buffer.append(delimiter);
54 buffer.append(iter.next());
55 }
56 }
57 return buffer.toString();
58 }
59
60 @NotNull
61 public static <T extends CharSequence> String join(@NotNull T[] strings, @NotNull String delimiter)
62 {
63 int capacity = 0;
64 int delimLength = delimiter.length();
65 for (T value : strings)
66 capacity += value.length() + delimLength;
67
68 StringBuilder buffer = new StringBuilder(capacity);
69 boolean first = true;
70 for (T value : strings) {
71 if (!first) {
72 buffer.append(delimiter);
73 } else {
74 first = false;
75 }
76 buffer.append(value);
77 }
78 return buffer.toString();
79 }
80
81 @NotNull
82 public static String fromStream(@NotNull InputStream stream) throws IOException
83 {
84 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
85 StringBuilder sb = new StringBuilder();
86 String line;
87 while ((line = reader.readLine()) != null) {
88 sb.append(line);
89 }
90 return sb.toString();
91 }
92
93 public static int compare(@Nullable String s1, @Nullable String s2)
94 {
95 boolean null1 = s1 == null;
96 boolean null2 = s2 == null;
97
98 if (null1 && null2) {
99 return 0;
100 } else if (null1) {
101 return -1;
102 } else if (null2) {
103 return 1;
104 } else {
105 return s1.compareTo(s2);
106 }
107 }
108
109 @NotNull
110 public static String urlEncode(@NotNull String name)
111 {
112 // Sufficient for now, it seems
113 return name.replace(" ", "%20");
114 }
115}
Note: See TracBrowser for help on using the repository browser.