Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String[] strings = new String[]{"first", "second"};
  3. System.out.println(Arrays.toString(strings));
  4. byte[][] byteStrings = convertToBytes(strings);
  5. strings = convertToStrings(byteStrings);
  6. System.out.println(Arrays.toString(strings));
  7.  
  8. }
  9.  
  10. private static String[] convertToStrings(byte[][] byteStrings) {
  11. String[] data = new String[byteStrings.length];
  12. for (int i = 0; i < byteStrings.length; i++) {
  13. data[i] = new String(byteStrings[i], Charset.defaultCharset());
  14.  
  15. }
  16. return data;
  17. }
  18.  
  19.  
  20. private static byte[][] convertToBytes(String[] strings) {
  21. byte[][] data = new byte[strings.length][];
  22. for (int i = 0; i < strings.length; i++) {
  23. String string = strings[i];
  24. data[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset
  25. }
  26. return data;
  27. }
  28.  
  29. final String[] stringArray = { "foo", "bar", "baz" };
  30.  
  31. final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  32. final ObjectOutputStream objectOutputStream =
  33. new ObjectOutputStream(byteArrayOutputStream);
  34. objectOutputStream.writeObject(stringArray);
  35. objectOutputStream.flush();
  36. objectOutputStream.close();
  37.  
  38. final byte[] byteArray = byteArrayOutputStream.toByteArray();
  39.  
  40. final ByteArrayInputStream byteArrayInputStream =
  41. new ByteArrayInputStream(byteArray);
  42. final ObjectInputStream objectInputStream =
  43. new ObjectInputStream(byteArrayInputStream);
  44.  
  45. final String[] stringArray2 = (String[]) objectInputStream.readObject();
  46.  
  47. objectInputStream.close();
  48.  
  49. import java.nio.ByteBuffer;
  50. import java.util.ArrayList;
  51.  
  52. public class Serialization {
  53. public static byte[] serialize(String[] strs) {
  54. ArrayList<Byte> byteList = new ArrayList<Byte>();
  55. for (String str: strs) {
  56. int len = str.getBytes().length;
  57. ByteBuffer bb = ByteBuffer.allocate(4);
  58. bb.putInt(len);
  59. byte[] lenArray = bb.array();
  60. for (byte b: lenArray) {
  61. byteList.add(b);
  62. }
  63. byte[] strArray = str.getBytes();
  64. for (byte b: strArray) {
  65. byteList.add(b);
  66. }
  67. }
  68. byte[] result = new byte[byteList.size()];
  69. for (int i=0; i<byteList.size(); i++) {
  70. result[i] = byteList.get(i);
  71. }
  72. return result;
  73. }
  74.  
  75. public static String[] unserialize(byte[] bytes) {
  76. ArrayList<String> strList = new ArrayList<String>();
  77. for (int i=0; i< bytes.length;) {
  78. byte[] lenArray = new byte[4];
  79. for (int j=i; j<i+4; j++) {
  80. lenArray[j-i] = bytes[j];
  81. }
  82. ByteBuffer wrapped = ByteBuffer.wrap(lenArray);
  83. int len = wrapped.getInt();
  84. byte[] strArray = new byte[len];
  85. for (int k=i+4; k<i+4+len; k++) {
  86. strArray[k-i-4] = bytes[k];
  87. }
  88. strList.add(new String(strArray));
  89. i += 4+len;
  90. }
  91. return strList.toArray(new String[strList.size()]);
  92. }
  93.  
  94. public static void main(String[] args) {
  95. String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
  96. byte[] byteArray = serialize(input);
  97. String[] output = unserialize(byteArray);
  98. for (String str: output) {
  99. System.out.println(str);
  100. }
  101. }
  102. }
  103.  
  104. public class Concatenation {
  105. public static byte[] concatenate(String[] strs) {
  106. StringBuilder sb = new StringBuilder();
  107. for (int i=0; i<strs.length; i++) {
  108. sb.append(strs[i]);
  109. if (i != strs.length-1) {
  110. sb.append("*.*"); //concatenate by this splitter
  111. }
  112. }
  113. return sb.toString().getBytes();
  114. }
  115.  
  116. public static String[] split(byte[] bytes) {
  117. String entire = new String(bytes);
  118. return entire.split("\*\.\*");
  119. }
  120.  
  121. public static void main(String[] args) {
  122. String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
  123. byte[] byteArray = concatenate(input);
  124. String[] output = split(byteArray);
  125. for (String str: output) {
  126. System.out.println(str);
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement