What kind of Service should I define for ".thrift"-file to use it later for my Program?
This File Transport should be between the Client and the Server and it should be "partly".
StreamFileService.thrift:
struct FileChunk {
1: binary data
2: i64 remaining
}
service StreamFileService {
FileChunk getBytes(1:string fileName, 2: i64 offset, 3: i32 size);
}
StreamFileClient.java:
public class StreamFileClient {
private int fileChunkSize = 16;
private String filePath;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
private void invoke() {
try {
TTransport theClientTransport = new TFramedTransport(new TSocket(
"127.0.0.1", 7911));
TProtocol theProtocol = new TBinaryProtocol(theClientTransport);
StreamFileService.Client theClient = new StreamFileService.Client(
theProtocol);
theClientTransport.open();
filePath = "/home/output/output.pdf";
File theFile2 = new File(filePath);
theFile2.createNewFile();
FileInputStream stream = new FileInputStream(theFile2);
long currentPosition = 0;
FileChannel theFileChannel = stream.getChannel();
boolean again = true;
do {
FileChunk chunk2 = theClient.getBytes(filePath,
currentPosition, fileChunkSize);
currentPosition += fileChunkSize;
theFileChannel.write(chunk2.data);
if (chunk2.remaining == 0)
again = false;
} while (again);
stream.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
StreamFileClient theClient = new StreamFileClient();
theClient.invoke();
}
}
StreamFileServer.java:
public class StreamFileServer {
private void start() {
try {
TNonblockingServerTransport theServerSocket = new TNonblockingServerSocket(
7911);
StreamFileService.Processor theProcessor = new StreamFileService.Processor(
new StreamFileServiceImpl());
TServer theServer = new TNonblockingServer(
new TNonblockingServer.Args(theServerSocket)
.processor(theProcessor));
System.out.println("Server starting on port 7911...");
theServer.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StreamFileServer theFileServer = new StreamFileServer();
theFileServer.start();
}
}
StreamFileServiceImpl:
public class StreamFileServiceImpl implements StreamFileService.Iface {
public FileChunk getBytes(String filePath, long offset, int size)
throws TException {
File theFile = new File("/home/input/kl_12.pdf");
FileChunk chunk = new FileChunk();
try {
FileOutputStream stream = new FileOutputStream(theFile);
MappedByteBuffer buffer = stream.getChannel().map(
FileChannel.MapMode.READ_ONLY, offset, size);
chunk.data = buffer;
chunk.remaining = stream.getChannel().size() - offset - size;
stream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return chunk;
}
}
Your code looks not so bad to me (not tested) and there is not much to change.
How about
typedef binary binar
service StreamFileService {
binar getBytes(1:string fileName, 2: i64 offset, 3: i32 size);
i64 getSize(1:string fileName)
}
I would also return a struct holding the bytes, but that's more or less my personal opinion.
struct FileChunk {
1: binary data
2: i64 remaining
}
service StreamFileService {
FileChunk getBytes(1:string fileName, 2: i64 offset, 3: i32 size);
}
The FileChunk struct can be easily extended, if such becomes necessary, e.g. in order to return additional metadata, like total size (especially if the size grows/shrinks over time), remaining bytes, indications about the data format, or the like. You don't have to do that, as you can easily extend the interface if such becomes necessary later. Matter of taste.
Related
Expected: When i run application in debug mode and pulling the endpoint, bytes still appear to be null however i did implement ApplicationEvent and passed ApplicationStartedEvent, then I have override onApplicationEvent and called my method there, which should lead to code execution once application started and bytes should already have a value. Have I missed something
public class FaqAttachment implements ApplicationListener<ApplicationStartedEvent> {
private final String fileName = "FAQ.pdf";
private byte[] bytes;
public Attachment asAttachment() {
return new Attachment(pdfToBytes(), fileName);
}
private byte[] pdfToBytes() {
if (bytes == null) {
try (FileInputStream inputStream = new FileInputStream(new File(ClassLoader.getSystemResource(fileName).getFile()))) {
this.bytes = ByteStreams.toByteArray(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return bytes;
}
#Override
public void onApplicationEvent(ApplicationStartedEvent event) {
pdfToBytes();
}
This should work :
#Component
public class FaqAttachment {
private final String fileName = "FAQ.pdf";
private byte[] bytes;
public Attachment asAttachment() {
return new Attachment(pdfToBytes(), fileName);
}
private byte[] pdfToBytes() {
if (bytes == null) {
try (FileInputStream inputStream = new FileInputStream(new File(ClassLoader.getSystemResource(fileName).getFile()))) {
this.bytes = ByteStreams.toByteArray(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return bytes;
}
#EventListener
public void onApplicationStartedEvent(ApplicationStartedEvent event) {
pdfToBytes();
}
}
I have written a Netty.io system in which I can easily send text files back and forth, but now I do not only want to send text files at the byte level, but if I announce before that comes a jar, even send a jar file at byte level.
Now I have tried only to send them like a normal file where logically a corrupt jar comes out.
So I send the text files:
public void sendFile(Channel channel, File files, String path) {
new Thread(new Runnable() {
#Override
public void run() {
sendFileInfo(channel,files,path);
while (file.containsKey(files.getName())) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
FileInputStream fis = new FileInputStream(files);
byte[] bytes = new byte[512];
int i =0;
int length;
while((length =fis.read(bytes))>0){
PacketOutFile512Bytes bit = new PacketOutFile512Bytes(i,files.getName(),length,bytes);
channel.writeAndFlush(bit);
if(!cache.containsKey(files.getName()))
cache.put(files.getName(),new HashMap<>());
HashMap<Integer, Timer> timecache = cache.get(files.getName());
timecache.put(i,new Timer());
timecache.get(i++).schedule(new TimerTask() {
#Override
public void run() {
channel.writeAndFlush(bit);
}
},Main.getInstance().getTimeout()*1000,Main.getInstance().getTimeout()*1000);
Thread.sleep(100);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public void writeFile(Channel channel, String file, int length){
new Thread(new Runnable() {
#Override
public void run() {
File sending = new File(path.get(file),file);
try {
sending.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
for(int i =0; i< amountofPackets.get(file);i++)
fos.write(cache.get(file).get(i),0,length);
fos.close();
channel.writeAndFlush(new PacketOutFileSucess(file));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Now I am looking for a method how to copy a jar on level of bytes that I can send with the same package.
This is how the packet looks for the byte snippet:
import de.letsplaybar.fileserver.ValueSizeException;
import lombok.Getter;
public class PacketInFile512Bytes implements Packet {
private #Getter int packetId;
private #Getter String name;
private #Getter int length;
private #Getter byte[] value;
public PacketInFile512Bytes() {
value = new byte[512];
}
public PacketInFile512Bytes(int packetId, String name, int length, byte[] value) throws ValueSizeException {
if(value.length != 512)
throw new ValueSizeException();
this.packetId = packetId;
this.name = name;
this.length = length;
this.value = value;
}
#Override
public void read(PacketSerilizer serilizer) {
packetId = serilizer.readInt();
name = serilizer.readString();
length = serilizer.readInt();
for (int i = 0; i<512;i++)
value[i] = serilizer.readByte();
}
#Override
public void write(PacketSerilizer serilizer) {
serilizer.writeInt(packetId);
serilizer.writeString(name);
serilizer.writeInt(length);
serilizer.writeBytes(value);
}
}
Who knows a way how I can do that?
Ever thank you in advance.
Letsplaybar
Hi I am trying to implement my own classifier in a java .Here is what I have gotten so far:
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
//instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
Now the problem is I cant get it to classify the data in the dataset (as good or bad).
I need help in trying to access a single instance in this code.
You could use instanceOf operator as below:
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++) {
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad")) {
countB++;
} else {
countA++;
}
}
package dm;
//import javax.activation.DataSource;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.*;
import weka.core.*;
import org.jfree.data.*;
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
int total=instances.numInstances();
System.out.println("total"+total);
Attribute attr= instances.attribute(16);
System.out.println("attr"+attr);
// checking class
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++)
{
pred=0;
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad"))
{
countB++;
}
else {
countA++;
}
}
System.out.println("good instances"+countA);
System.out.println("bad instances"+ countB);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void buildClassifier (Instances data)
{
}
public double classifyInstance (Instance inst)
{
return 0;
}
}
How are the values of static variables persisted during serialization(If at all persisted). I have read similar questions on stack where it says that static variables are inherently transient i.e their state or current values is not serialized.
I was just doing a very simple example where i serialized a class and saved it to a file and then again reconstructed the class from the file.Surprisingly I find that the value of the static variable at and when the serialization happened is saved.
How does this happen. Is this because the class template along with it's instance information is saved during serialization. Here is the code snippet -
public class ChildClass implements Serializable, Cloneable{
/**
*
*/
private static final long serialVersionUID = 5041762167843978459L;
private static int staticState;
int state = 0;
public ChildClass(int state){
this.state = state;
staticState = 10001;
}
public String toString() {
return "state" + state + " " + "static state " + staticState;
}
public static void setStaticState(int state) {
staticState = state;
}
and here is my main class
public class TestRunner {
/**
* #param args
*/
public static void main(String[] args) {
new TestRunner().run();
}
public TestRunner() {
}
public void run() {
ChildClass c = new ChildClass(101);
ChildClass.setStaticState(999999);
FileOutputStream fo = null;
ObjectOutputStream os = null;
File file = new File("F:\\test");
try {
fo = new FileOutputStream(file);
os = new ObjectOutputStream(fo);
os.writeObject(c);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(null != os)os.close();
if(null != fo) fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileInputStream fi = null;
ObjectInputStream ois = null;
ChildClass streamed;
try {
fi = new FileInputStream(file);
ois = new ObjectInputStream(fi);
Object o = ois.readObject();
if(o instanceof ChildClass){
streamed = (ChildClass)o;
//ChildClass cloned = streamed.clone();
System.out.println(streamed.toString());
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(null != ois)ois.close();
if(null != fi) fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note : There is nothing wrong with the code. I just am wondering how the value of the static variable 'staticState' in the class 'ChildClass' gets saved. Will the state be saved if i transmit this serialized data over a network then
The static field value was not serialized. The output is printing the new value of the static field simply because you modified it to 999999 but you never reset its value to the old one before de-serizalizing. Since the field is static, the new value is reflected in any instance of ChildClass.
To properly assert that the field is not serialized, reset the value to 10001 before de-serializing the object, and you will notice that its value is not 999999.
...
ChildClass.setStaticState(10001);
FileInputStream fi = null;
ObjectInputStream ois = null;
ChildClass streamed;
...
// when de-serializing, the below will print "state101 static state 10001"
System.out.println(streamed.toString());
1.Problem on emulator:
I am launching my midlet app at first time is to store some data and then I am restarting it at second time is to read the stored data. It is working well in first two cases without any exception.
However I am restarting it second time on the same way then It gives exception: "Uncaught exception java/lang/NumberFormatException:" it is processing only char and total data is less than 64 kb.
2.Problem on real device:
RMS is not working at all. I don't know if I need to give a permission for the handset (nokia n95).
Thanks.
In app, it is only storing charity companies into rms according to a selected country. So if a country is already selected, it must skip country list and then display company list in every restart.
In below code, rms_Check() method is to check the data in order to open country or company list frame.
public class X {
private static RecordStore rs =null;
private static Vector rms_Vector = new Vector();
static final String REC_STORE ="db_1";
public X() {
}
public void openRecStore(){
try {
rs = RecordStore.openRecordStore(REC_STORE, true);
System.out.println("open record store");
} catch (Exception e)
{
db(e.toString()+" in openRecStore");
}
}
public void closeRecStore(){
try {
rs.closeRecordStore();
} catch (Exception e) {
db(e.toString()+" in closeRecStore");
}
}
public void deleteRecStore()
{
if (RecordStore.listRecordStores()!=null){
try {
RecordStore.deleteRecordStore(REC_STORE);
} catch (Exception e) {
db(e.toString()+" in deleteRecStore");
}
}
}
public void writeRecord(String str) throws UnsupportedEncodingException
{
byte[] rec = str.getBytes("UTF-8");
try {
rs.addRecord(rec, 0, rec.length);
System.out.println("write record store");
} catch (Exception e) {
db(e.toString()+" in writeRecord");
}
}
public void readRecord()
{
try {
// Intentionally it is too small to test code
byte[] m_enc = new byte[5];
byte[] recData = new String(m_enc).getBytes("UTF-8");
int len;
for(int i =1; i<= rs.getNumRecords(); i++){
if(rs.getRecordSize(i)> recData.length)
recData = new byte[rs.getRecordSize(i)];
len = rs.getRecord(i, recData, 0);
System.out.println("Record #"+i+":"+new String(recData, 0, len));
System.out.println("------------------------");
rms_Vector.addElement(new String(recData, 0, len));
}
} catch (Exception e) {
db(e.toString() +" in readStore");
}
}
private void db(String str)
{
System.out.println("Msg:"+str);
}
public Vector rms_Array(){
return this.rms_Vector;
}
public boolean rms_Check(){
if(this.rms_Vector.size()>0){
System.out.print("rms_check: true");
// if true it will display company list every time
return true;
}else{
System.out.print("rms_check: false");
//if false it will display country list then company list
return false;
}
}
}
Use this
private RecordStore rs = null; // Record store
public String REC_STORE = "RSM name"; // Name of record store
public int record_max=0;
public void openRecStore(){
try{
rs = RecordStore.openRecordStore(REC_STORE, true );
}catch (Exception e){}
}
public void closeRecStore(){
try{
rs.closeRecordStore();
}catch (Exception e){}
}
public void deleteRecStore(){
if (RecordStore.listRecordStores() != null){
try{
RecordStore.deleteRecordStore(REC_STORE);
}catch (Exception e){}
}
}
public void writeRecord(String str){
byte[] rec = str.getBytes();
try{
rs.addRecord(rec, 0, rec.length);
}catch (Exception e){}
}
public void readRecords(){
try{
byte[] recData = new byte[5];
int len;
record_max=rs.getNumRecords();
for(int i = 1; i <= record_max; i++){
if(rs.getRecordSize(i) > recData.length){
recData = new byte[rs.getRecordSize(i)];
}
len = rs.getRecord(i, recData, 0);
file_name[i]=new String(recData, 0, len);
}
}catch (Exception e){}
}
you have file_name[] array of save data
for load actin commad use :
openRecStore();
readRecords();
for(int j=1;j<=record_max;j++ ) {
System.out.println("Record " + j + " : " + file_name[j]);
}
closeRecStore();
and save this :
openRecStore();
writeRecord(textField.getString());
closeRecStore();