Skip to content

Instantly share code, notes, and snippets.

@tatesuke
Last active November 29, 2015 02:41
Show Gist options
  • Save tatesuke/6fd0c50e2a0c24616ea4 to your computer and use it in GitHub Desktop.
Save tatesuke/6fd0c50e2a0c24616ea4 to your computer and use it in GitHub Desktop.
ContourGPSが生成した動画ファイルからnmeaを抜き出す。
package com.tatesuke.contour;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PushbackInputStream;
public class NmeaExporter {
public static void main(String[] args) throws FileNotFoundException, IOException {
if (args.length == 0) {
usage();
System.exit(1);
}
try (PushbackInputStream is = new PushbackInputStream(new FileInputStream(args[0]), 256)) {
export(is);
} catch (Throwable t) {
t.printStackTrace();
}
}
private static void export(PushbackInputStream is) throws IOException {
while (true) {
byte[] buf = new byte[6];
// ファイル終端に達したら終了
int bytes = is.read(buf);
if (bytes < buf.length) {
break;
}
// 規定の文字じゃなければスキップ
if (!equals(buf, "$GPGGA") && !equals(buf, "$GPRMC")) {
is.unread(buf, 1, buf.length - 1);
continue;
}
// 改行文字(\r\n)まで出力
System.out.print(new String(buf));
while (true) {
buf = new byte[2];
is.read(buf);
if (equals(buf, "\r\n")) {
break;
}
System.out.print((char) buf[0]);
is.unread(buf, 1, 1);
}
System.out.println("");
}
}
private static boolean equals(byte[] buf, String str) {
if (buf.length != str.length()) {
return false;
}
for (int i = 0; i < buf.length; i++) {
if ((char) buf[i] != str.charAt(i)) {
return false;
}
}
return true;
}
private static void usage() {
System.err.println("ファイルパスプリーズ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment