1919import javax .annotation .Nullable ;
2020import java .io .File ;
2121import java .nio .charset .Charset ;
22- import java .nio .charset .StandardCharsets ;
2322import java .nio .file .Path ;
2423import java .util .ArrayList ;
2524import java .util .Collections ;
@@ -69,22 +68,30 @@ public class MediaType implements Comparable<MediaType> {
6968
7069 public static final MediaType all = new MediaType (ALL , null );
7170
72- private final String value ;
71+ private final String raw ;
7372
7473 private final Charset charset ;
7574
7675 private final int subtypeStart ;
7776
7877 private final int subtypeEnd ;
7978
79+ private final String value ;
80+
8081 private MediaType (@ Nonnull String value , Charset charset ) {
81- this .value = value ;
82+ this .raw = value ;
8283 this .subtypeStart = value .indexOf ('/' );
8384 if (subtypeStart < 0 ) {
8485 throw new IllegalArgumentException ("Invalid media type: " + value );
8586 }
8687 int subtypeEnd = value .indexOf (';' );
87- this .subtypeEnd = subtypeEnd < 0 ? value .length () : subtypeEnd ;
88+ if (subtypeEnd < 0 ) {
89+ this .value = raw ;
90+ this .subtypeEnd = value .length ();
91+ } else {
92+ this .value = raw .substring (0 , subtypeEnd );
93+ this .subtypeEnd = subtypeEnd ;
94+ }
8895 this .charset = charset ;
8996 }
9097
@@ -97,21 +104,21 @@ private MediaType(@Nonnull String value, Charset charset) {
97104 }
98105
99106 @ Override public int hashCode () {
100- return value .hashCode ();
107+ return raw .hashCode ();
101108 }
102109
103110 public @ Nullable String param (@ Nonnull String name ) {
104111 int paramStart = subtypeEnd + 1 ;
105- for (int i = subtypeEnd ; i < value .length (); i ++) {
106- char ch = value .charAt (i );
112+ for (int i = subtypeEnd ; i < raw .length (); i ++) {
113+ char ch = raw .charAt (i );
107114 if (ch == '=' ) {
108- String pname = value .substring (paramStart , i ).trim ();
109- int paramValueEnd = value .indexOf (';' , i );
115+ String pname = raw .substring (paramStart , i ).trim ();
116+ int paramValueEnd = raw .indexOf (';' , i );
110117 if (paramValueEnd < 0 ) {
111- paramValueEnd = value .length ();
118+ paramValueEnd = raw .length ();
112119 }
113120 if (pname .equals (name )) {
114- return value .substring (i + 1 , paramValueEnd ).trim ();
121+ return raw .substring (i + 1 , paramValueEnd ).trim ();
115122 }
116123 paramStart = paramValueEnd + 1 ;
117124 i = paramStart ;
@@ -121,18 +128,18 @@ private MediaType(@Nonnull String value, Charset charset) {
121128 }
122129
123130 public @ Nonnull String value () {
124- return value . substring ( 0 , subtypeEnd ) ;
131+ return value ;
125132 }
126133
127- public @ Nonnull String toContenTypeHeader (@ Nullable Charset charset ) {
134+ public @ Nonnull String toContentTypeHeader (@ Nullable Charset charset ) {
128135 if (charset == null ) {
129136 Charset paramCharset = charset ();
130137 if (paramCharset == null ) {
131- return value () ;
138+ return value ;
132139 }
133140 charset = paramCharset ;
134141 }
135- return value () + ";charset=" + charset .name ();
142+ return value + ";charset=" + charset .name ();
136143 }
137144
138145 @ Nonnull public float quality () {
@@ -176,19 +183,19 @@ private Charset _charset(Charset charset) {
176183 }
177184
178185 public @ Nonnull String type () {
179- return value .substring (0 , subtypeStart ).trim ();
186+ return raw .substring (0 , subtypeStart ).trim ();
180187 }
181188
182189 public @ Nonnull String subtype () {
183- return value .substring (subtypeStart + 1 , subtypeEnd ).trim ();
190+ return raw .substring (subtypeStart + 1 , subtypeEnd ).trim ();
184191 }
185192
186193 public boolean matches (@ Nonnull String contentType ) {
187- return matches (value () , contentType );
194+ return matches (value , contentType );
188195 }
189196
190197 public boolean matches (@ Nonnull MediaType type ) {
191- return matches (type .value () );
198+ return matches (value , type .value );
192199 }
193200
194201 public int score () {
@@ -204,8 +211,8 @@ public int score() {
204211
205212 public int paramSize () {
206213 int p = 0 ;
207- for (int i = subtypeEnd ; i < value .length (); i ++) {
208- char ch = value .charAt (i );
214+ for (int i = subtypeEnd ; i < raw .length (); i ++) {
215+ char ch = raw .charAt (i );
209216 if (ch == '=' ) {
210217 p += 1 ;
211218 }
@@ -247,7 +254,7 @@ public int paramSize() {
247254 return new MediaType (value , null );
248255 }
249256
250- public static @ Nonnull List <MediaType > fromAcceptHeader (@ Nullable String value ) {
257+ public static @ Nonnull List <MediaType > parse (@ Nullable String value ) {
251258 if (value == null || value .length () == 0 ) {
252259 return Collections .emptyList ();
253260 }
@@ -739,6 +746,6 @@ private static boolean matchOne(String expected, int len1, String contentType) {
739746 }
740747
741748 @ Override public String toString () {
742- return value ;
749+ return raw ;
743750 }
744751}
0 commit comments