Skip to content

Commit 386189e

Browse files
committed
Optimization of ContentType parsing
1 parent 1b159d1 commit 386189e

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

httpcore5/src/main/java/org/apache/hc/core5/http/ContentType.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import java.util.LinkedHashMap;
3939
import java.util.List;
4040
import java.util.Map;
41-
import java.util.concurrent.atomic.AtomicReference;
4241

4342
import org.apache.hc.core5.annotation.Contract;
4443
import org.apache.hc.core5.annotation.ThreadingBehavior;
44+
import org.apache.hc.core5.http.message.BasicHeaderValueParser;
4545
import org.apache.hc.core5.http.message.BasicNameValuePair;
4646
import org.apache.hc.core5.http.message.MessageSupport;
4747
import org.apache.hc.core5.http.message.ParserCursor;
@@ -434,15 +434,31 @@ private static ContentType parse(final CharSequence s, final boolean strict) thr
434434
return null;
435435
}
436436
final ParserCursor cursor = new ParserCursor(0, s.length());
437-
final AtomicReference<HeaderElement> firstElementRef = new AtomicReference<>();
438-
MessageSupport.parseElements(s, cursor, e -> firstElementRef.compareAndSet(null, e));
439-
final HeaderElement element = firstElementRef.get();
437+
return parse(s, cursor, strict);
438+
}
439+
440+
private static ContentType parse(final CharSequence s, final ParserCursor cursor, final boolean strict) throws UnsupportedCharsetException {
441+
final HeaderElement element = BasicHeaderValueParser.INSTANCE.parseHeaderElement(s, cursor);
440442
if (element != null) {
441443
return create(element, strict);
442444
}
443445
return null;
444446
}
445447

448+
/**
449+
* @since 5.5
450+
*/
451+
public static ContentType parse(final CharSequence s, final ParserCursor cursor) throws UnsupportedCharsetException {
452+
return parse(s, cursor, true);
453+
}
454+
455+
/**
456+
* @since 5.5
457+
*/
458+
public static ContentType parseLenient(final CharSequence s, final ParserCursor cursor) throws UnsupportedCharsetException {
459+
return parse(s, cursor, false);
460+
}
461+
446462
/**
447463
* Returns {@code Content-Type} for the given MIME type.
448464
*

0 commit comments

Comments
 (0)