/** * Returns a new request body that transmits {@code content}. If {@code * contentType} lacks a charset, this will use UTF-8. */ public static Body create(MediaType contentType, String content) { contentType = contentType.charset() != null ? contentType : MediaType.parse(contentType + "; charset=utf-8"); try { byte[] bytes = content.getBytes(contentType.charset().name()); return create(contentType, bytes); } catch (UnsupportedEncodingException e) { throw new AssertionError(); } }
/** * Returns a media type for {@code string}, or null if {@code string} is not a * well-formed media type. */ public static MediaType parse(String string) { Matcher typeSubtype = TYPE_SUBTYPE.matcher(string); if (!typeSubtype.lookingAt()) return null; String type = typeSubtype.group(1).toLowerCase(Locale.US); String subtype = typeSubtype.group(2).toLowerCase(Locale.US); String charset = null; Matcher parameter = PARAMETER.matcher(string); for (int s = typeSubtype.end(); s < string.length(); s = parameter.end()) { parameter.region(s, string.length()); if (!parameter.lookingAt()) return null; // This is not a well-formed media type. String name = parameter.group(1); if (name == null || !name.equalsIgnoreCase("charset")) continue; if (charset != null) throw new IllegalArgumentException("Multiple charsets: " + string); charset = parameter.group(2) != null ? parameter.group(2) // Value is a token. : parameter.group(3); // Value is a quoted string. } return new MediaType(string, type, subtype, charset); }
/** * Returns a media type for {@code string}, or null if {@code string} is not a * well-formed media type. */ public static MediaType parse(String string) { Matcher typeSubtype = TYPE_SUBTYPE.matcher(string); if (!typeSubtype.lookingAt()) return null; String type = typeSubtype.group(1).toLowerCase(Locale.US); String subtype = typeSubtype.group(2).toLowerCase(Locale.US); String charset = null; Matcher parameter = PARAMETER.matcher(string); for (int s = typeSubtype.end(); s < string.length(); s = parameter.end()) { parameter.region(s, string.length()); if (!parameter.lookingAt()) return null; // This is not a well-formed media type. String name = parameter.group(1); if (name == null || !name.equalsIgnoreCase("charset")) continue; if (charset != null) throw new IllegalArgumentException("Multiple charsets: " + string); charset = parameter.group(2) != null ? parameter.group(2) // Value is a token. : parameter.group(3); // Value is a quoted string. } return new MediaType(string, type, subtype, charset); }
/** * Returns a new request body that transmits {@code content}. If {@code * contentType} lacks a charset, this will use UTF-8. */ public static Body create(MediaType contentType, String content) { contentType = contentType.charset() != null ? contentType : MediaType.parse(contentType + "; charset=utf-8"); try { byte[] bytes = content.getBytes(contentType.charset().name()); return create(contentType, bytes); } catch (UnsupportedEncodingException e) { throw new AssertionError(); } }