I have trouble using Hastable in this class:
public class HttpBuilder {
...
private int ret;
public Hashtable headers;
private String content;
HttpBuilder(int majorv, int minorv, int ret){
ver[0] = majorv;
ver[1] = minorv;
this.ret = ret;
headers = new Hashtable();
}
...
public void addHeader(String header, String value){
headers.put(header, value);
}
...
}
This class builds a string from multiple input parameters. I use it in multiple threads. Something like this:
HttpBuilder Get(HttpParser request) {
HttpBuilder response;
String doc;
if (request.getRequestURL().equals("/")) {
try {
doc = LoadDoc("main.html");
} catch (IOException e) {
response = new HttpBuilder(1, 1, 500);
return response;
}
response = new HttpBuilder(1, 1, 200);
response.addHeader("content-type", "text/html");
response.setContent(doc);
} else {
response = new HttpBuilder(1, 1, 404);
}
return response;
}
After addHeader Hashtable is empty.
Consume data:
public String toString() {
String result;
int len = 0;
result = "HTTP/"+Integer.toString(ver[0])+"."+Integer.toString(ver[1])+
" "+getHttpReply(ret)+"\n";
if(content!=null){
len = content.length();
if(len!=0){
headers.put("content-length", Integer.toString(len));
}
}
Iterator it = headers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
result += pairs.getKey() + ": " + pairs.getValue() + "\n";
it.remove();
}
if(len!=0){
result+="\n"+content;
}
return result;
}
Thread class where i use HttpBuilder
class ClientThread implements Runnable {
private Socket socket;
private ServerData data;
static public final String NotImplemented = "HTTP/1.1 501 Not Implemented";
static public final String NotFound = "HTTP/1.1 404 Not Found";
ClientThread(Socket socket, ServerData data) {
this.socket = socket;
this.data = data;
}
#Override
public void run() {
try {
HttpParser request = new HttpParser(socket.getInputStream());
HttpBuilder response;
if (request.parseRequest() != 200) {
response = new HttpBuilder(1, 1, 501);
} else {
if (request.getMethod().equals("GET")) {
response = Get(request);
} else if (request.getMethod().equals("POST")) {
response = Post(request);
} else {
response = new HttpBuilder(1, 1, 400);
}
}
} catch (IOException e) {
Server.log.log(Level.SEVERE, e.getLocalizedMessage());
} finally {
try {
socket.close();
Server.log.log(Level.INFO, "Close connection");
} catch (IOException e) {
Server.log.log(Level.SEVERE, e.getLocalizedMessage());
}
}
}
void send(String response) throws IOException {
PrintWriter out;
out = new PrintWriter(socket.getOutputStream(), true);
out.print(response);
}
String LoadDoc(String doc) throws IOException {
final String Folder = "web" + File.separator;
String result = null;
doc = Folder + doc;
long len;
File f = new File(doc);
FileReader fr = new FileReader(f);
len = f.length();
char[] buffer = new char[(int) len];
fr.read(buffer);
result = new String(buffer);
fr.close();
return result;
}
HttpBuilder Get(HttpParser request) {
HttpBuilder response;
String doc;
if (request.getRequestURL().equals("/")) {
try {
doc = LoadDoc("main.html");
} catch (IOException e) {
response = new HttpBuilder(1, 1, 500);
return response;
}
response = new HttpBuilder(1, 1, 200);
response.addHeader("content-type", "text/html");
response.setContent(doc);
} else {
response = new HttpBuilder(1, 1, 404);
}
return response;
}
HttpBuilder Post(HttpParser request) {
HttpBuilder response;
String str;
if(request.getRequestURL().equals("/")){
response = new HttpBuilder(1,1, 200);
str = request.getContentParam("user");
response.setContent(str+" added to the base.");
}else {
response = new HttpBuilder(1, 1, 404);
}
return response;
}
}
It seems a bad idea to modify your object in toString().
The purpose of toString() is to return a String representation of your object. Multiple subsequent calls to toString() should return the same result.
When you iterate over the headers in toString() you remove the headers :
Iterator it = headers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
result += pairs.getKey() + ": " + pairs.getValue() + "\n";
it.remove();
}
If that's a desired behavior, I suggest you use a method with a different name for this logic.
Since toString() overrides a method of Object, it's possible that it's called somewhere where you're not expecting it to be called, and empties your headers map.
Your debugger calls toString on the Hashtable, so you see the values displayed. But calling this method also removes the values, so viewing it in the debugger actually empties the table. This is a Bad Idea, your toString method should not modify the object.
And your HttpBuilder isn't thread safe, because you use a Hashtable. Luckily, you don't call it from multiple threads, at least, not in the code you have posted.
Related
I create Java Application using HttpServer as bellow:
public class Application
{
public static void main(String args[])
{
HttpServer httpPaymentServer;
httpPaymentServer = HttpServer.create(new InetSocketAddress(Config.portPayment), 0);
httpPaymentServer.createContext("/json", new Payment("json"));
}
public class Payment implements HttpHandler
{
public Payment(String dataType)
{
}
public void handle(HttpExchange httpExchange) throws IOException
{
String body = "";
if(httpExchange.getRequestMethod().equalsIgnoreCase("POST"))
{
try
{
Headers requestHeaders = httpExchange.getRequestHeaders();
Set<Map.Entry<String, List<String>>> entries = requestHeaders.entrySet();
int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));
InputStream inputStream = httpExchange.getRequestBody();
byte[] postData = new byte[contentLength];
int length = inputStream.read(postData, 0, contentLength);
if(length < contentLength)
{
}
else
{
String fullBody = new String(postData);
Map<String, String> query = Utility.splitQuery(fullBody);
body = query.getOrDefault("data", "").toString();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
On my server (Centos 7), on the first request, it is no problem. But on next request, not all of the request body can be read.
But on my PC (Windows 10) no problem.
What is the problem.
For your InputStream you call read only once - it may not return all the data. That data may even be not received at that time.
Instead you should call read in a loop until you get all the bytes (when you reach end of stream read returns -1). Or use one of the approaches suggested here How to read / convert an InputStream into a String in Java?
Thank you. This work for me
public void handle(HttpExchange httpExchange) throws IOException
{
String body = "";
if(httpExchange.getRequestMethod().equalsIgnoreCase("POST"))
{
try
{
Headers requestHeaders = httpExchange.getRequestHeaders();
Set<Map.Entry<String, List<String>>> entries = requestHeaders.entrySet();
int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));
InputStream inputStream = httpExchange.getRequestBody();
int j;
String fullBody = "";
for(j = 0; j < contentLength; j++)
{
byte b = (byte) httpExchange.getRequestBody().read();
fullBody += String.format("%c", b);
}
Map<String, String> query = Utility.splitQuery(fullBody);
body = query.getOrDefault("data", "").toString();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
I have a piece of software that generates SOAP-requests based on an excel-file, and then emails the results.
Due to the potential size of the requests, I do the soap-request-handling in parallel. The following code handles the above mentioned.
public void HandleData() {
List<NodeAnalysisReply> replies = Collections.synchronizedList(new ArrayList<>());
new Thread(() -> {
List<NodeAnalysisRequest> requests;
SOAPMessageFactory factory = new SOAPMessageFactory();
SOAPResponseParser parser = new SOAPResponseParser();
try {
requests = new ExcelParser().parseData(file);
requests.parallelStream().forEach((request) -> {
try {
SOAPMessage message = factory.createNodeRequestMessage(
new RequestObject(requestInfoFactory.makeInfo(trackingID), request));
SOAPMessage response = new SoapConnector(server.getUrl()).executeRequest(message);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
NodeAnalysisReply curReply = parser.ParseXMLResponse(out.toString(), request);
synchronized (replies) {
System.out.println("Adding: " + curReply.getRequest().toString());
replies.add(curReply);
}
} catch (UnsupportedOperationException | SOAPException | IOException e) {
handleSoap(e.getMessage());
}
});
} catch (IOException e) {
handleBadParse();
}
try {
for(NodeAnalysisReply reply : replies){
System.out.println("Data: " + reply.getRequest().toString());
}
mailer.SendEmail("Done", email, replies);
} catch (MessagingException e) {
e.printStackTrace();
}
}).start();
}
When I run the code with two piece of data, the following happens:
Adding: Søndergade 52 6920 // OK
Adding: Ternevej 1 6920 // OK
Data: Ternevej 1 6920 // What
Data: Ternevej 1 6920 // WHAT..
are equal? true
So even though it adds both items to the list, it seems like the last one takes both places. How come is that, and how do I solve it? - I really do miss the Parrallel.ForEach() form C#!
EDIT: As requested, the code for NodeAnalysisReply.
public class NodeAnalysisReply {
public ReplyInfo getReplyInfo() {
return replyInfo;
}
public void setReplyInfo(ReplyInfo replyInfo) {
this.replyInfo = replyInfo;
}
public List < nodeAnalysisListDetails > getNodeAnalysisListDetails() {
return nodeAnalysisListDetails;
}
public void setNodeAnalysisListDetails(List < nodeAnalysisListDetails > nodeAnalysisListDetails) {
this.nodeAnalysisListDetails = nodeAnalysisListDetails;
}
public void addNodeAnalysisListDetail(nodeAnalysisListDetails nodeAnalysisListDetails) {
this.nodeAnalysisListDetails.add(nodeAnalysisListDetails);
}
ReplyInfo replyInfo;
public String getFormattedXML() {
return formattedXML;
}
public void setFormattedXML(String formattedXML) {
this.formattedXML = formattedXML;
}
String formattedXML;
public NodeAnalysisRequest getRequest() {
return request;
}
public void setRequest(NodeAnalysisRequest request) {
this.request = request;
}
NodeAnalysisRequest request;
List < nodeAnalysisListDetails > nodeAnalysisListDetails = new ArrayList < > ();
}
synchronized (replies) {
System.out.println("Adding: " + curReply.getRequest().toString());
replies.add(curReply);
}
The above code in a lambda of stream is called a side effect and is not encouraged at all.
What you should do is something like below.
replies.addAll(requests.parallelStream().map((request) -> {
try {
SOAPMessage message = factory.createNodeRequestMessage(
new RequestObject(requestInfoFactory.makeInfo(trackingID), request));
SOAPMessage response = new SoapConnector(server.getUrl()).executeRequest(message);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
NodeAnalysisReply curReply = parser.ParseXMLResponse(out.toString(), request);
return curReply;
} catch (UnsupportedOperationException | SOAPException | IOException e) {
handleSoap(e.getMessage());
return null;
}
})
.filter(curReply -> curReply != null)
.collect(Collectors.toList())
);
In the Above code you map each request to a NodeAnalysisReply first and then filter only the non null values and finally you Collect it into a list and all those to your replies list.
My program is laid out so that the main app can send commands to any node connected to it. When a node receives a request, it returns a response and continues to wait for more requests.
When the app is run the node successfully replies to one request, and when a second request is sent the node sees it as a null or does not see it at all. Why does this keep happening?
P.S. I want the connection to the node to stay open, so that it can receive more requests.
Request sending code:
public java.lang.String getTime(server.Node node){
protocol.Message ms = new protocol.Message("<time>","");
node.sendRequestToClient(ms);
node.dos.flush();
java.lang.System.out.println("Sent time request to " + node.ip);
java.lang.String time = null;
try {
time = node.dis.readLine();
} catch (java.io.IOException ex) {
System.out.println("Could not read response.");
}
protocol.Message response = protocol.Message.parseDataToMessage(time);
java.lang.String systime = response.getActionData();
return systime;
}
Response sending code:
public class Client {
public Client(NetworkConnection connection){
this.connectionToServer = connection;
try{
connectionToServer.connect();
responseOutStream = connectionToServer.getPrintWriter();
requestInStream = connectionToServer.getBufferedReader();
}catch(IOException ex){
System.out.println("Could not connect to server." + ex.getMessage() + ex.toString());
}
}
public void beginRequestListener(){
String request;
try {
while((request = requestInStream.readLine())!=""){
System.out.println("Recieved request: " + request + request.length());
Message response = Message.parseDataToMessage(request);
sendResponseToServer(response);
}
} catch (java.io.IOException ex) {
System.out.println("Could not read request stream.");
} catch(NullPointerException e){
e.printStackTrace();
e.getClass();
}
}
public void sendResponseToServer(Message ms){
protocol.Message response = MessageParser.compileResponse(ms);
java.lang.System.out.println("Response to send: "+response);
response.send(responseOutStream);
}
public BufferedReader requestInStream;
public PrintWriter responseOutStream;
public NetworkConnection connectionToServer;
}
MessageParser class:
public class MessageParser {
static public Message compileResponse(Message ms){
Message response = null;
switch(ms.getAction()){
case "<time>":
response = new Message("<time>", String.valueOf(System.currentTimeMillis()));
break;
case "<date>":
SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
Date date = new Date();
sd.setTimeZone(TimeZone.getTimeZone("IST"));
response = new Message("<date>", date.toString());
break;
default:
break;
}
return response;
}
}
The stack trace and output:
Recieved request: <action><time><action><actionData><actionData>
Response to send: <action><time><action><actionData>1370380854566<actionData>
Recieved request:
java.lang.NullPointerException
at protocol.MessageParser.compileResponse(MessageParser.java:23)
at client.Client.sendResponseToServer(Client.java:67)
at client.Client.beginRequestListener(Client.java:52)
at client.ClientInterface.main(ClientInterface.java:107)
Message class:
public class Message {
public Message(String data){
}
public Message(String action, String actionData){
this.action = action;
this.actionData = actionData;
}
public void send(PrintWriter connection){
try{
connection.println(this.toString());
connection.flush();
//System.out.println(this.toString());
}catch(Exception ex){
System.out.println("Could not send Message.");
}
}
#java.lang.Override
public String toString(){
return
action_marker + action + action_marker +
actionData_marker + actionData + actionData_marker +
eof_marker;
}
public static Message parseDataToMessage(String data){
Message ms = null;
if(data.isEmpty() == false){
int begin_action_marker = data.indexOf("<action>")+8;
int end_action_marker = data.lastIndexOf("<action>");
String action = data.substring(begin_action_marker, end_action_marker);
int begin_actionData_marker = data.indexOf("<actionData>")+12;
int end_actionData_marker = data.lastIndexOf("<actionData>");
String actionData = data.substring(begin_actionData_marker, end_actionData_marker);
ms = new Message(action, actionData);
}
return ms;
}
public void setAction(String action){
this.action = action;
}
public String getActionData(){
return actionData;
}
public String getAction(){
return action;
}
public void setActionData(String action){
this.actionData = action;
}
public String eof_marker = "\r\n";
public String action;
public String action_marker = "<action>";
public String actionData;
public String actionData_marker = "<actionData>";
}
My guess:
you receive an empty request in (request = requestInStream.readLine())
this goes to Message.parseDataToMessage(request); which returns null for empty requests
that generates a NullPointerException in compileResponse
The (likely) solution: change this
while((request = requestInStream.readLine())!=""){
into this:
while(!(request = requestInStream.readLine()).isEmpty())
Why your code does not work: How do I compare strings in Java?
while((request = requestInStream.readLine())!=""){
What's this test for? Are you expecting empty requests? You shouldn't be. If you get one it's a bug at the sender.
However you must test the result of readLine() for null before doing anything else with it. The line should read:
while((request = requestInStream.readLine())!= null){
Does anyone know where to find a little how to on using dbpedia spotlight in java or scala? Or could anyone explain how it's done? I can't find any information on this...
The DBpedia Spotlight wiki pages would be a good place to start.
And I believe the installation page has listed the most popular ways (using a jar, or set up a web service) to use the application.
It includes instructions on using the Java/Scala API with your own installation, or calling the Web Service.
There are some additional data needed to be downloaded to run your own server for full service, good time to make a coffee for yourself.
you need download dbpedia spotlight (jar file) after that u can use next two classes ( author pablomendes ) i only make some change .
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org:80/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
private static String powered_by ="non";
private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
private static String showScores ="yes";
#SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT,
String powered_by,String spotter,String disambiguator,String showScores){
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
this.powered_by=powered_by;
this.spotter=spotter;
this.disambiguator=disambiguator;
this.showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
+ "&spotter=" + spotter
+ "&disambiguator=" + disambiguator
+ "&showScores=" + showScores
+ "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("#URI"),
Integer.parseInt(entity.getString("#support"))));
} catch (JSONException e) {
LOG.error("JSON exception "+e);
}
}
return resources;
}
}
second class
/**
* #author pablomendes
*/
public abstract class AnnotationClient {
public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(HttpMethod method) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
LOG.error("Fatal transport error: " + e.getMessage());
LOG.error(method.getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
method.releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
#SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
main()
public static void main(String[] args) throws Exception {
String Question ="Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
System.out.println("resource : "+c.getResu());
}
I just add one little fix for your answer.
Your code is running, if you add the evaluate method call:
public static void main(String[] args) throws Exception {
String question = "Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
Lamine
In the request method of the second class (AnnotationClient) in Adel's answer, the author Pablo Mendes hasn't finished
TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
which is an annoying warning that needs to be removed by replacing
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
with
Reader in = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");
StringWriter writer = new StringWriter();
org.apache.commons.io.IOUtils.copy(in, writer);
response = writer.toString();
I am writing an app for Android that grabs meta data from SHOUTcast mp3 streams. I am using a pretty nifty class I found online that I slightly modified, but I am still having 2 problems.
1) I have to continuously ping the server to update the metadata using a TimerTask. I am not fond of this approach but it was all I could think of.
2) There is a metric tonne of garbage collection while my app is running. Removing the TimerTask got rid of the garbage collection issue so I am not sure if I am just doing it wrong or if this is normal.
Here is the class I am using:
public class IcyStreamMeta {
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
public IcyStreamMeta(URL streamUrl) {
setStreamUrl(streamUrl);
isError = false;
}
/**
* Get artist using stream's title
*
* #return String
* #throws IOException
*/
public String getArtist() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
return title.trim();
}catch (StringIndexOutOfBoundsException e) {
return "";
}
}
/**
* Get title using stream's title
*
* #return String
* #throws IOException
*/
public String getTitle() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
String artist = streamTitle.substring(streamTitle.indexOf("-")+1);
return artist.trim();
} catch (StringIndexOutOfBoundsException e) {
return "";
}
}
public Map<String, String> getMetadata() throws IOException {
if (metadata == null) {
refreshMeta();
}
return metadata;
}
public void refreshMeta() throws IOException {
retreiveMetadata();
}
private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
//con.setRequestProperty("Accept", null);
con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();
if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char)stream.read()) != -1) {
strHeaders.append(c);
if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}
// Match headers to get metadata offset within a stream
Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}
// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}
// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after headers
while ((b = stream.read()) != -1) {
count++;
// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}
if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {
inData = true;
} else {
inData = false;
}
if (inData) {
if (b != 0) {
metaData.append((char)b);
}
}
if (count > (metaDataOffset + metaDataLength)) {
break;
}
}
// Set the data
metadata = IcyStreamMeta.parseMetadata(metaData.toString());
// Close
stream.close();
}
public boolean isError() {
return isError;
}
public URL getStreamUrl() {
return streamUrl;
}
public void setStreamUrl(URL streamUrl) {
this.metadata = null;
this.streamUrl = streamUrl;
this.isError = false;
}
public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap<String, String>();
String[] metaParts = metaString.split(";");
Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
Matcher m;
for (int i = 0; i < metaParts.length; i++) {
m = p.matcher(metaParts[i]);
if (m.find()) {
metadata.put((String)m.group(1), (String)m.group(2));
}
}
return metadata;
}
}
And here is my timer:
private void getMeta() {
timer.schedule(new TimerTask() {
public void run() {
try {
icy = new IcyStreamMeta(new URL(stationUrl));
runOnUiThread(new Runnable() {
public void run() {
try {
artist.setText(icy.getArtist());
title.setText(icy.getTitle());
} catch (IOException e) {
e.printStackTrace();
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
},0,5000);
}
Much appreciation for any assistance!
I've replaced the IcyStreamMeta class in my program and am getting the meta data from the 7.html file that is a part of the SHOUTcast spec. Far less data usage and all that so I feel it is a better option.
I am still using the TimerTask, which is acceptable. There is practically no GC any more and I am happy with using 7.html and a little regex. :)