-
-
Save autronix/6c60e2dd7876845bc2ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.data; | |
import java.net.*; | |
import java.io.*; | |
import java.util.*; | |
import java.util.zip.*; | |
import java.text.*; | |
import org.apache.commons.codec.binary.Hex; | |
public class SampleDataAppExample { | |
static final char MSG_DELIM = '\u001F'; | |
static final int MAX_DATA_SIZE = 100000; | |
static final int SEQ_LEN = 10; | |
static final int BLOCK_SIZE_LEN = 4; | |
public static void main(String argv[]) { | |
Socket socket = null; | |
Inflater inflater = new Inflater(true); | |
byte[] compressedData = new byte[MAX_DATA_SIZE]; | |
byte[] uncompressedData = new byte[MAX_DATA_SIZE]; | |
try { | |
String host = "host.example.com"; | |
int port = 9999; | |
String dataSourceName = "SERVICE_REQUEST"; | |
if (argv != null && argv.length > 0 && argv[0] != null) { | |
dataSourceName = argv[0]; | |
} | |
socket = new Socket(host,port); | |
DataInputStream in = new DataInputStream(socket.getInputStream()); | |
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |
System.out.println("Connected to: "+host+":"+port); | |
String request = "\r\nGET " + dataSourceName +"?id=-1\r\n"; | |
out.print(request); | |
out.flush(); | |
System.out.println("Sent: "+request); | |
for (int chunks=0; chunks<10; chunks++) { | |
long chunkTime = in.readLong(); | |
int sequence = in.readInt(); | |
int dataSize = in.readInt(); | |
int receivedSize = 0; | |
for ( int readSize; (readSize = in.read(compressedData, receivedSize, dataSize - receivedSize)) != -1 && (receivedSize += readSize) < dataSize; ); | |
if (receivedSize != dataSize) { | |
System.err.println("expected block of " + dataSize + ", received " + receivedSize); | |
} | |
inflater.reset(); | |
inflater.setInput(compressedData, 0, receivedSize); | |
int resultLength = inflater.inflate(uncompressedData); | |
System.out.println("Data size: "+receivedSize+ " - bytes: "+Hex.encodeHexString(compressedData)+"\nResultsize: "+ resultLength+" - "+Hex.encodeHexString(uncompressedData)); | |
/* Additional procession of the data here */ | |
} | |
socket.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
if (socket != null) { | |
try { | |
socket.close(); | |
} catch (Exception e2) { | |
e2.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment