Skip to content

Commit 06d1f3e

Browse files
committed
Merge branch '__rultor'
2 parents 4311a16 + 9c7ad49 commit 06d1f3e

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

src/main/java/com/g4s8/mime/MimeType.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.g4s8.mime;
2525

2626
import java.io.IOException;
27-
import java.util.List;
2827
import java.util.Map;
2928

3029
/**
@@ -55,5 +54,5 @@ public interface MimeType {
5554
* @return Parameter map
5655
* @throws IOException If failed to read
5756
*/
58-
Map<String, List<String>> params() throws IOException;
57+
Map<String, String> params() throws IOException;
5958
}

src/main/java/com/g4s8/mime/MimeTypeOf.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
import java.io.IOException;
2727
import java.util.HashMap;
28-
import java.util.LinkedList;
29-
import java.util.List;
3028
import java.util.Locale;
3129
import java.util.Map;
3230
import java.util.regex.Matcher;
@@ -91,20 +89,22 @@ public String subtype() throws IOException {
9189

9290
@Override
9391
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
94-
public Map<String, List<String>> params() throws IOException {
92+
public Map<String, String> params() throws IOException {
9593
final Matcher match = this.matcher();
9694
final Matcher param = MimeTypeOf.PTN_PARAM.matcher(this.src);
97-
final Map<String, List<String>> map = new HashMap<>(1);
95+
final Map<String, String> map = new HashMap<>(1);
9896
for (int id = match.end(); id < this.src.length(); id = param.end()) {
9997
param.region(id, this.src.length());
10098
if (!param.lookingAt()) {
10199
throw new IOException("Invalid mime-type params format");
102100
}
103101
final String name = param.group(1);
104-
if (!map.containsKey(name)) {
105-
map.put(name, new LinkedList<String>());
102+
if (map.containsKey(name)) {
103+
throw new IOException(
104+
String.format("Parameter %s may only exist once.", name)
105+
);
106106
}
107-
map.get(name).add(MimeTypeOf.paramValue(param));
107+
map.put(name, MimeTypeOf.paramValue(param));
108108
}
109109
return map;
110110
}

src/main/java/com/g4s8/mime/MimeTypeSmart.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.g4s8.mime;
2525

2626
import java.io.IOException;
27-
import java.util.List;
2827
import java.util.Map;
2928

3029
/**
@@ -67,7 +66,7 @@ public String subtype() throws IOException {
6766
}
6867

6968
@Override
70-
public Map<String, List<String>> params() throws IOException {
69+
public Map<String, String> params() throws IOException {
7170
return this.orig.params();
7271
}
7372

@@ -77,18 +76,15 @@ public Map<String, List<String>> params() throws IOException {
7776
* @throws IOException If not found or invalid.
7877
*/
7978
public String charset() throws IOException {
80-
final Map<String, List<String>> params = this.params();
79+
final Map<String, String> params = this.params();
8180
if (!params.containsKey(MimeTypeSmart.PARAM_CHARSET)) {
8281
throw new IOException("Charset is not provided");
8382
}
84-
final List<String> charsets = params.get(MimeTypeSmart.PARAM_CHARSET);
85-
if (charsets.isEmpty()) {
86-
throw new IOException("Empty charset param");
83+
final String charset = params.get(MimeTypeSmart.PARAM_CHARSET);
84+
if (charset == null || "".equals(charset)) {
85+
throw new IOException("The charset parameter is not set");
8786
}
88-
if (charsets.size() > 1) {
89-
throw new IOException("More than one charset");
90-
}
91-
return charsets.get(0);
87+
return charset;
9288
}
9389

9490
@Override

src/test/java/com/g4s8/mime/MimeTypeOfTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ public void param() throws IOException {
6464
"Can't read params",
6565
new MimeTypeOf("multipart/byteranges; boundary=3d6b6a416f9b5")
6666
.params()
67-
.get("boundary")
68-
.get(0),
67+
.get("boundary"),
6968
Matchers.equalTo("3d6b6a416f9b5")
7069
);
7170
}
7271

72+
@Test(expected = IOException.class)
73+
public void sameParamTwice() throws IOException {
74+
new MimeTypeOf("multipart/byteranges; a=1; a=1")
75+
.params();
76+
}
77+
7378
@Test
7479
public void asString() {
7580
final String src = "text/plain";

src/test/java/com/g4s8/mime/MimeTypeSmartTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public void testCharset() throws IOException {
4949
);
5050
}
5151

52+
@Test(expected = IOException.class)
53+
public void testNoCharset() throws IOException {
54+
new MimeTypeSmart(
55+
new MimeTypeOf("text/html")
56+
).charset();
57+
}
58+
5259
@Test
5360
public void asString() {
5461
final String src = "application/octet-stream";

0 commit comments

Comments
 (0)