https://github.com/g4s8/mime
Media types (MIME types) RFC6838
https://github.com/g4s8/mime
java-library mime-types oop-library
Last synced: 9 months ago
JSON representation
Media types (MIME types) RFC6838
- Host: GitHub
- URL: https://github.com/g4s8/mime
- Owner: g4s8
- License: mit
- Created: 2017-09-26T17:34:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-19T23:56:31.000Z (over 1 year ago)
- Last Synced: 2025-04-09T22:53:03.832Z (9 months ago)
- Topics: java-library, mime-types, oop-library
- Language: Java
- Size: 164 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.header
Awesome Lists containing this project
README
MIME is a java library without dependencies for parsing and creating
HTTP media-type objects, according to [RFC6838](https://tools.ietf.org/html/rfc6838) specifications.
[](https://mvnrepository.com/artifact/wtf.g4s8/mime)
[](https://github.com/g4s8/mime/actions/workflows/ci-checks.yml)
[](https://github.com/g4s8/mime/blob/master/LICENSE.txt)
[](https://codecov.io/github/g4s8/mime?branch=master)
## Usage
MIME type has mandatory type and subtype, e.g. `application/json` has `application` type
and `json` subtype. Some MIME types has additional parameters, e.g. `text/plain; encoding=utf-8` has
`encoding` param with `utf-8` value.
For all these entities there are related method exist. To construct MIME parser uses `MimeType.of` function:
```java
var mime = MimeType.of("text/xml; encoding=utf-8");
var type = mime.type(); // "text"
var subtype = mime.subtype(); // "xml"
var encoding = mime.param("encoding"); // "utf-8"
```
To parse multi-value mime-type strings (like `Accept` header value), use `MimeType.parse(String)` method, it pays attention to qualifier
of each value and sorts mime types using this qualifier.
*Example:*
```java
MimeType.parse("text/html;q=0.6, application/xml;q=0.9, image/bmp;q=0.3,image/bmp;q=0.5, text/html, multipart/mixed, text/json;q=0.4")
```
returns a lits of:
1. `multipart/mixed`
2. `text/html`
3. `application/xml`
4. `image/bmp`
5. `text/json`
## Testing
[Hamcrest](http://hamcrest.org/JavaHamcrest/) matchers are included in `test` package. Them can be used to verify mime-types:
```java
// verify type
MatcherAssert.assertThat(
MimeType.of("application/pdf"),
new HmMimeHasType("application")
);
// verify subtype
MatcherAssert.assertThat(
MimeType.of("image/bmp"),
new HmMimeHasSubType("bmp")
);
// verify parameter
MatcherAssert.assertThat(
MimeType.of("image/bmp; charset=utf-8"),
new HmMimeHasParameter("charset", "utf-8")
);
```
## Contributing
If you are interested in contributing please refer to [CONTRIBUTING.md](CONTRIBUTING.md)