Skip to content

Instantly share code, notes, and snippets.

@ktuukkan
Created March 27, 2014 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktuukkan/9812607 to your computer and use it in GitHub Desktop.
Save ktuukkan/9812607 to your computer and use it in GitHub Desktop.
/*
* ChunkedUDPDataReader.java
* Copyright (C) 2014 Kimmo Tuukkanen
*
* This file is part of Java Marine API.
* <http://ktuukkan.github.io/marine-api/>
*
* Java Marine API is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Java Marine API is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Java Marine API. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.marineapi.nmea.io;
import java.net.DatagramSocket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/**
* Prototype reader for "chunked" NMEA over UDP, i.e. multiple sentences being
* delivered in single packet.
*
* @author Kimmo Tuukkanen
*/
public class ChunkedUDPDataReader extends UDPDataReader {
private Queue<String> buffer = new LinkedList<String>();
public ChunkedUDPDataReader(DatagramSocket socket, SentenceReader parent) {
super(socket, parent);
}
/* (non-Javadoc)
* @see net.sf.marineapi.nmea.io.AbstractDataReader#read()
*/
@Override
public String read() {
String data = super.read();
if(data != null) {
String[] lines = data.split("\\r?\\n");
buffer.addAll(Arrays.asList(lines));
}
return buffer.poll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment