Java Useragent ANalyzer
A fast, thread-safe and dependency-free user agent parser for Java 8+.
Usage
Simply add the library to the dependency section of your pom.xml (if you're using Maven):
<dependency>
<groupId>com.github.codesorcery</groupId>
<artifactId>juan</artifactId>
<version>0.1.1</version>
</dependency>
Or add it to your build.gradle (if you're using Gradle):
dependencies {
compile('com.github.codesorcery:juan:0.1.1')
}
The library can then be used as follows:
import com.github.codesorcery.juan.UserAgentParser;
import com.github.codesorcery.juan.ParsedUserAgent;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
public class UserAgentParserExample {
private final UserAgentParser userAgentParser;
public UserAgentParserExample() throws IOException {
URL deviceList =
new URL("http://storage.googleapis.com/play_public/supported_devices.csv");
userAgentParser =
UserAgentParser.withPlayStoreDeviceList(deviceList, Charset.forName("UTF-16"));
}
public void parsingExample() {
String userAgentString =
"Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/60.0.3112.107 Mobile Safari/537.36";
ParsedUserAgent parsed =
userAgentParser.parse(userAgentString);
parsed.agent().getVendor();
parsed.agent().getName();
parsed.agent().getVersion();
parsed.agent().getType();
parsed.os().getVendor();
parsed.os().getName();
parsed.os().getVersion();
parsed.os().getType();
parsed.device().getVendor();
parsed.device().getName();
}
}
Benchmark
Benchmark performed on an Intel® Core™ 2 Duo P8600 CPU @ 2.4GHz:
Benchmark | Mode | Cnt | Score | Error | Units |
---|
UserAgentParserBenchmark.amazonKindleFireSilkBrowser | avgt | 5 | 4.641 | ± 0.136 | us/op |
UserAgentParserBenchmark.curl | avgt | 5 | 1.699 | ± 0.069 | us/op |
UserAgentParserBenchmark.iPadMobileSafari | avgt | 5 | 6.370 | ± 0.266 | us/op |
UserAgentParserBenchmark.iPhoneInstagram | avgt | 5 | 5.257 | ± 1.164 | us/op |
UserAgentParserBenchmark.linuxFirefox | avgt | 5 | 3.522 | ± 0.120 | us/op |
UserAgentParserBenchmark.macOsSafari | avgt | 5 | 3.126 | ± 0.022 | us/op |
UserAgentParserBenchmark.samsungGalaxyS8ChromeMobile | avgt | 5 | 5.375 | ± 0.102 | us/op |
UserAgentParserBenchmark.windows10Edge | avgt | 5 | 3.962 | ± 0.065 | us/op |
UserAgentParserBenchmark.windowsPhone | avgt | 5 | 9.555 | ± 0.190 | us/op |