json
This commit is contained in:
snoop 2017-05-17 14:11:13 +09:00
parent ee3701a18c
commit a8f1008055
2 changed files with 63 additions and 11 deletions

View File

@ -12,10 +12,7 @@ import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import com.loafle.overflow.crawler.Crawler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.*;
import java.lang.Boolean;
import java.util.HashMap;
import java.util.List;
@ -120,8 +117,10 @@ public class Server {
try {
Object obj = crawler.get(req.getId());
writefff((byte[])obj);
Output reply = Output.newBuilder()
.setData(ByteString.copyFrom(convertObjectToBytes(obj)))
.setData(ByteString.copyFrom((byte[])obj))
.setStartDate(333)
.setEndDate(333)
.build();
@ -132,6 +131,23 @@ public class Server {
}
}
}
public void writefff(byte[] aaa) {
try {
FileOutputStream fos = new FileOutputStream("/home/cm2/1212.txt");
fos.write(aaa);
fos.close();
BufferedWriter out = new BufferedWriter(new FileWriter("/home/cm2/1212str.txt"));
out.write(new String(aaa,0,aaa.length));
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class ConfigImpl extends ConfigGrpc.ConfigImplBase {

View File

@ -11,12 +11,15 @@ import io.grpc.ManagedChannelBuilder;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
@ -91,6 +94,7 @@ public class ServerTest {
}
@Ignore
@Test
public void TestAdd() throws Exception {
init();
@ -119,6 +123,7 @@ public class ServerTest {
}
@Ignore
@Test
public void TestRemove() throws Exception {
init();
@ -144,6 +149,7 @@ public class ServerTest {
}
@Ignore
@Test
public void TestGet() throws Exception {
@ -154,17 +160,47 @@ public class ServerTest {
setId("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734").
setName(Crawlers.REDIS).build();
Output out = client.get(input);
// Output out = client.get(input);
ByteArrayInputStream bis = new ByteArrayInputStream(out.getData().toByteArray());
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject();
//byte[] aaa = out.getData().toByteArray();
logger.info("!!!!!!!!!!!!!!!! " + o);
// ByteArrayInputStream bis = new ByteArrayInputStream(out.getData().toByteArray());
//
// ObjectInput in = new ObjectInputStream(bis);
// Object o = in.readObject();
//
// ObjectMapper mapper = new ObjectMapper();
// String str = new String(aaa);
// HashMap<String, Object> rs = new ObjectMapper().readValue(str, HashMap.class) ;
assertEquals("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734",o);
// System.out.println(str);
// logger.info("!!!!!!!!!!!!!!!! " + out.getData().toByteArray());
// assertEquals("SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734",o);
}
@Test
public void TestByte() throws Exception {
Map<String, String> aa = new HashMap<>();
aa.put("2313", "asdf");
ObjectMapper mapper = new ObjectMapper();
String aaa = mapper.writeValueAsString(aa);
System.out.println(aaa);
Object bbb = aaa.getBytes();
}
}