'How to convert a Java string into a ByteString without re-escaping the string
I'm trying to convert an already java encoded string into a ByteString, without re-encoding the string.
My method looks like this:
import com.google.api.HttpBody;
import com.google.protobuf.ByteString;
protected HttpBody getHttpBody(String contentType, String data) {
System.out.println("=======d==:" + data);
HttpBody.Builder bodyBuilder = HttpBody.newBuilder();
bodyBuilder.setContentType(contentType);
try {
bodyBuilder.setData(ByteString.copyFrom(data.getBytes()));
} catch (Exception e) {}
HttpBody body = bodyBuilder.build();
System.out.println("=======d2==:" + body);
return body;
}
The input "data" looks like this:
=======d==:------WebKitFormBoundaryeyaPdl6AduTufZV4\r\nContent-Disposition: form-data; name=\"content\"; filename=\"473892_CRUST_grammars.zip\"\r\nContent-Type: application/x-zip-compressed\r\n\r\nPK\003\004\024\000\000\000\b\000\342:nT\222{\260q;\001\000\000\022\002\000\000\021\000\000\000en-US/CRUST.grxmleQOO\2030\024?\217OQ\353\201\023+h4s\026\226\005\227x\232\211l1\272L\322\2617\326\004ZR\212\250\237\336\256\262\211YO\257\357\375\376\275\226N>\313\002}\200\252\271\024\241\033\f}\027\201\310\344\226\213<ty\362\344\215F7w^\340N\"\207\346\212\225%S\310P\306\0053\000\f\302[&\370\304\307\206\217\221\222R\2078~^&\v|\300\212:\304{\255\2531!m\333\016\333\353\241T9\271\362\375\200\370\267\244\023\305\221\203\272CK\320\f\tVB\210\353\226+\310R!U\311\n\376\r\251\226i\245\344\206mx\3015\207\032\243L\n\r\3028\006\230D\2163\240\252)\000\361\355)B\235\311\312HU\315\246\340\231\361\031\f\250\024\340\311\335\237%2=\256\241\214,Y\301\0165\212\207\3702\333\003\324_F\227j\226G\364b\025?L\027\323\225\0256\372\217\263Y\362j/\357\261E\242X5\265\036\317\337\322\376l>{A\3759\276_\257#J\254$\261\266\207L\344\030\312\224\207\024\377w\351\222\364\"\233\207:[\303v\255\"\372%\240\243A\037p\356D\217\337\0209?PK\001\002\024\000\024\000\000\000\b\000\342:nT\222{\260q;\001\000\000\022\002\000\000\021\000\000\000\000\000\000\000\001\000 \000\000\000\000\000\000\000en-US/CRUST.grxmlPK\005\006\000\000\000\000\001\000\001\000?\000\000\000j\001\000\000\000\000\r\n------WebKitFormBoundaryeyaPdl6AduTufZV4--\r\n
While the data after being converted to ByteString looks like this:
=======d2==:content_type: "multipart/form-data; boundary=----WebKitFormBoundaryeyaPdl6AduTufZV4"
data: "------WebKitFormBoundaryeyaPdl6AduTufZV4\\r\\nContent-Disposition: form-data; name=\\\"content\\\"; filename=\\\"473892_CRUST_grammars.zip\\\"\\r\\nContent-Type: application/x-zip-compressed\\r\\n\\r\\nPK\\003\\004\\024\\000\\000\\000\\b\\000\\342:nT\\222{\\260q;\\001\\000\\000\\022\\002\\000\\000\\021\\000\\000\\000en-US/CRUST.grxmleQOO\\2030\\024?\\217OQ\\353\\201\\023+h4s\\026\\226\\005\\227x\\232\\211l1\\272L\\322\\2617\\326\\004ZR\\212\\250\\237\\336\\256\\262\\211YO\\257\\357\\375\\376\\275\\226N>\\313\\002}\\200\\252\\271\\024\\241\\033\\f}\\027\\201\\310\\344\\226\\213<ty\\362\\344\\215F7w^\\340N\\\"\\207\\346\\212\\225%S\\310P\\306\\0053\\000\\f\\302[&\\370\\304\\307\\206\\217\\221\\222R\\2078~^&\\v|\\300\\212:\\304{\\255\\2531!m\\333\\016\\333\\353\\241T9\\271\\362\\375\\200\\370\\267\\244\\023\\305\\221\\203\\272CK\\320\\f\\tVB\\210\\353\\226+\\310R!U\\311\\n\\376\\r\\251\\226i\\245\\344\\206mx\\3015\\207\\032\\243L\\n\\r\\3028\\006\\230D\\2163\\240\\252)\\000\\361\\355)B\\235\\311\\312HU\\315\\246\\340\\231\\361\\031\\f\\250\\024\\340\\311\\335\\237%2=\\256\\241\\214,Y\\301\\0165\\212\\207\\3702\\333\\003\\324_F\\227j\\226G\\364b\\025?L\\027\\323\\225\\0256\\372\\217\\263Y\\362j/\\357\\261E\\242X5\\265\\036\\317\\337\\322\\376l>{A\\3759\\276_\\257#J\\254$\\261\\266\\207L\\344\\030\\312\\224\\207\\024\\377w\\351\\222\\364\\\"\\233\\207:[\\303v\\255\\\"\\372%\\240\\243A\\037p\\356D\\217\\337\\0209?PK\\001\\002\\024\\000\\024\\000\\000\\000\\b\\000\\342:nT\\222{\\260q;\\001\\000\\000\\022\\002\\000\\000\\021\\000\\000\\000\\000\\000\\000\\000\\001\\000 \\000\\000\\000\\000\\000\\000\\000en-US/CRUST.grxmlPK\\005\\006\\000\\000\\000\\000\\001\\000\\001\\000?\\000\\000\\000j\\001\\000\\000\\000\\000\\r\\n------WebKitFormBoundaryeyaPdl6AduTufZV4--\\r\\n"
What I want is for the 2 data fields to look the same. As you can see after the conversion to ByteString, the data content gets escaped again (which I don't want).
I've tried a couple of copyFrom /copyFromUtf8 approaches, providing encoding, even tried to unescape the input using StringEscapeUtils functionality, before passing it into copyFrom, but messes up the message structure.
This message is a multipart form data payload which contains a zip file with a text file in it. I need this in order to mock a request for some integration tests.
Any idea how I can achieve this ?
Thanks
Solution 1:[1]
OK for whoever will be interested, I've took a different approach to solve my problem. Basically I've serialized the whole HttpBody object and dumped it into a text file and then in my tests I've desterilized the content of the file. That worked.
protected HttpBody deserializeHttpBody(String file) {
try {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("data/" + file);
ObjectInputStream objectInputStream = new ObjectInputStream(in);
HttpBody obj = (HttpBody) objectInputStream.readObject();
objectInputStream.close();
return obj;
} catch (Exception e) {
System.out.println("==============Error==" + e);
}
return null;
}
private void serialize (com.google.api.HttpBody body) {
try {
FileOutputStream fileOutputStream
= new FileOutputStream("/tmp/upload"+System.currentTimeMillis()+".txt");
ObjectOutputStream objectOutputStream
= new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(body);
objectOutputStream.flush();
objectOutputStream.close();
System.out.println("==============Saved==");
} catch (Exception e) {
System.out.println("==============Error==" + e);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
