Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 4, 2017
2 parents 4311a16 + 9c7ad49 commit 06d1f3e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/g4s8/mime/MimeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.g4s8.mime;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -55,5 +54,5 @@ public interface MimeType {
* @return Parameter map
* @throws IOException If failed to read
*/
Map<String, List<String>> params() throws IOException;
Map<String, String> params() throws IOException;
}
14 changes: 7 additions & 7 deletions src/main/java/com/g4s8/mime/MimeTypeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -91,20 +89,22 @@ public String subtype() throws IOException {

@Override
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public Map<String, List<String>> params() throws IOException {
public Map<String, String> params() throws IOException {
final Matcher match = this.matcher();
final Matcher param = MimeTypeOf.PTN_PARAM.matcher(this.src);
final Map<String, List<String>> map = new HashMap<>(1);
final Map<String, String> map = new HashMap<>(1);
for (int id = match.end(); id < this.src.length(); id = param.end()) {
param.region(id, this.src.length());
if (!param.lookingAt()) {
throw new IOException("Invalid mime-type params format");
}
final String name = param.group(1);
if (!map.containsKey(name)) {
map.put(name, new LinkedList<String>());
if (map.containsKey(name)) {
throw new IOException(
String.format("Parameter %s may only exist once.", name)
);
}
map.get(name).add(MimeTypeOf.paramValue(param));
map.put(name, MimeTypeOf.paramValue(param));
}
return map;
}
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/g4s8/mime/MimeTypeSmart.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.g4s8.mime;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ public String subtype() throws IOException {
}

@Override
public Map<String, List<String>> params() throws IOException {
public Map<String, String> params() throws IOException {
return this.orig.params();
}

Expand All @@ -77,18 +76,15 @@ public Map<String, List<String>> params() throws IOException {
* @throws IOException If not found or invalid.
*/
public String charset() throws IOException {
final Map<String, List<String>> params = this.params();
final Map<String, String> params = this.params();
if (!params.containsKey(MimeTypeSmart.PARAM_CHARSET)) {
throw new IOException("Charset is not provided");
}
final List<String> charsets = params.get(MimeTypeSmart.PARAM_CHARSET);
if (charsets.isEmpty()) {
throw new IOException("Empty charset param");
final String charset = params.get(MimeTypeSmart.PARAM_CHARSET);
if (charset == null || "".equals(charset)) {
throw new IOException("The charset parameter is not set");
}
if (charsets.size() > 1) {
throw new IOException("More than one charset");
}
return charsets.get(0);
return charset;
}

@Override
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/com/g4s8/mime/MimeTypeOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ public void param() throws IOException {
"Can't read params",
new MimeTypeOf("multipart/byteranges; boundary=3d6b6a416f9b5")
.params()
.get("boundary")
.get(0),
.get("boundary"),
Matchers.equalTo("3d6b6a416f9b5")
);
}

@Test(expected = IOException.class)
public void sameParamTwice() throws IOException {
new MimeTypeOf("multipart/byteranges; a=1; a=1")
.params();
}

@Test
public void asString() {
final String src = "text/plain";
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/g4s8/mime/MimeTypeSmartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public void testCharset() throws IOException {
);
}

@Test(expected = IOException.class)
public void testNoCharset() throws IOException {
new MimeTypeSmart(
new MimeTypeOf("text/html")
).charset();
}

@Test
public void asString() {
final String src = "application/octet-stream";
Expand Down

0 comments on commit 06d1f3e

Please sign in to comment.