Commit 774c01d0 by aquilescanta Committed by Oliver Woodman

Make MimeType top level check related methods more robust

Throwing an IllegalArgumentException doesn't help the method signature.

PiperOrigin-RevId: 234579722
parent 8e7ae920
...@@ -120,42 +120,22 @@ public final class MimeTypes { ...@@ -120,42 +120,22 @@ public final class MimeTypes {
customMimeTypes.add(customMimeType); customMimeTypes.add(customMimeType);
} }
/** /** Returns whether the given string is an audio mime type. */
* Whether the top-level type of {@code mimeType} is audio.
*
* @param mimeType The mimeType to test.
* @return Whether the top level type is audio.
*/
public static boolean isAudio(@Nullable String mimeType) { public static boolean isAudio(@Nullable String mimeType) {
return BASE_TYPE_AUDIO.equals(getTopLevelType(mimeType)); return BASE_TYPE_AUDIO.equals(getTopLevelType(mimeType));
} }
/** /** Returns whether the given string is a video mime type. */
* Whether the top-level type of {@code mimeType} is video.
*
* @param mimeType The mimeType to test.
* @return Whether the top level type is video.
*/
public static boolean isVideo(@Nullable String mimeType) { public static boolean isVideo(@Nullable String mimeType) {
return BASE_TYPE_VIDEO.equals(getTopLevelType(mimeType)); return BASE_TYPE_VIDEO.equals(getTopLevelType(mimeType));
} }
/** /** Returns whether the given string is a text mime type. */
* Whether the top-level type of {@code mimeType} is text.
*
* @param mimeType The mimeType to test.
* @return Whether the top level type is text.
*/
public static boolean isText(@Nullable String mimeType) { public static boolean isText(@Nullable String mimeType) {
return BASE_TYPE_TEXT.equals(getTopLevelType(mimeType)); return BASE_TYPE_TEXT.equals(getTopLevelType(mimeType));
} }
/** /** Returns whether the given string is an application mime type. */
* Whether the top-level type of {@code mimeType} is application.
*
* @param mimeType The mimeType to test.
* @return Whether the top level type is application.
*/
public static boolean isApplication(@Nullable String mimeType) { public static boolean isApplication(@Nullable String mimeType) {
return BASE_TYPE_APPLICATION.equals(getTopLevelType(mimeType)); return BASE_TYPE_APPLICATION.equals(getTopLevelType(mimeType));
} }
...@@ -384,10 +364,8 @@ public final class MimeTypes { ...@@ -384,10 +364,8 @@ public final class MimeTypes {
} }
/** /**
* Returns the top-level type of {@code mimeType}. * Returns the top-level type of {@code mimeType}, or null if {@code mimeType} is null or does not
* * contain a forward slash character ({@code '/'}).
* @param mimeType The mimeType whose top-level type is required.
* @return The top-level type, or null if the mimeType is null.
*/ */
private static @Nullable String getTopLevelType(@Nullable String mimeType) { private static @Nullable String getTopLevelType(@Nullable String mimeType) {
if (mimeType == null) { if (mimeType == null) {
...@@ -395,7 +373,7 @@ public final class MimeTypes { ...@@ -395,7 +373,7 @@ public final class MimeTypes {
} }
int indexOfSlash = mimeType.indexOf('/'); int indexOfSlash = mimeType.indexOf('/');
if (indexOfSlash == -1) { if (indexOfSlash == -1) {
throw new IllegalArgumentException("Invalid mime type: " + mimeType); return null;
} }
return mimeType.substring(0, indexOfSlash); return mimeType.substring(0, indexOfSlash);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment