Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String[] strings = new String[]{"first", "second"};
- System.out.println(Arrays.toString(strings));
- byte[][] byteStrings = convertToBytes(strings);
- strings = convertToStrings(byteStrings);
- System.out.println(Arrays.toString(strings));
- }
- private static String[] convertToStrings(byte[][] byteStrings) {
- String[] data = new String[byteStrings.length];
- for (int i = 0; i < byteStrings.length; i++) {
- data[i] = new String(byteStrings[i], Charset.defaultCharset());
- }
- return data;
- }
- private static byte[][] convertToBytes(String[] strings) {
- byte[][] data = new byte[strings.length][];
- for (int i = 0; i < strings.length; i++) {
- String string = strings[i];
- data[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset
- }
- return data;
- }
- final String[] stringArray = { "foo", "bar", "baz" };
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final ObjectOutputStream objectOutputStream =
- new ObjectOutputStream(byteArrayOutputStream);
- objectOutputStream.writeObject(stringArray);
- objectOutputStream.flush();
- objectOutputStream.close();
- final byte[] byteArray = byteArrayOutputStream.toByteArray();
- final ByteArrayInputStream byteArrayInputStream =
- new ByteArrayInputStream(byteArray);
- final ObjectInputStream objectInputStream =
- new ObjectInputStream(byteArrayInputStream);
- final String[] stringArray2 = (String[]) objectInputStream.readObject();
- objectInputStream.close();
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- public class Serialization {
- public static byte[] serialize(String[] strs) {
- ArrayList<Byte> byteList = new ArrayList<Byte>();
- for (String str: strs) {
- int len = str.getBytes().length;
- ByteBuffer bb = ByteBuffer.allocate(4);
- bb.putInt(len);
- byte[] lenArray = bb.array();
- for (byte b: lenArray) {
- byteList.add(b);
- }
- byte[] strArray = str.getBytes();
- for (byte b: strArray) {
- byteList.add(b);
- }
- }
- byte[] result = new byte[byteList.size()];
- for (int i=0; i<byteList.size(); i++) {
- result[i] = byteList.get(i);
- }
- return result;
- }
- public static String[] unserialize(byte[] bytes) {
- ArrayList<String> strList = new ArrayList<String>();
- for (int i=0; i< bytes.length;) {
- byte[] lenArray = new byte[4];
- for (int j=i; j<i+4; j++) {
- lenArray[j-i] = bytes[j];
- }
- ByteBuffer wrapped = ByteBuffer.wrap(lenArray);
- int len = wrapped.getInt();
- byte[] strArray = new byte[len];
- for (int k=i+4; k<i+4+len; k++) {
- strArray[k-i-4] = bytes[k];
- }
- strList.add(new String(strArray));
- i += 4+len;
- }
- return strList.toArray(new String[strList.size()]);
- }
- public static void main(String[] args) {
- String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
- byte[] byteArray = serialize(input);
- String[] output = unserialize(byteArray);
- for (String str: output) {
- System.out.println(str);
- }
- }
- }
- public class Concatenation {
- public static byte[] concatenate(String[] strs) {
- StringBuilder sb = new StringBuilder();
- for (int i=0; i<strs.length; i++) {
- sb.append(strs[i]);
- if (i != strs.length-1) {
- sb.append("*.*"); //concatenate by this splitter
- }
- }
- return sb.toString().getBytes();
- }
- public static String[] split(byte[] bytes) {
- String entire = new String(bytes);
- return entire.split("\*\.\*");
- }
- public static void main(String[] args) {
- String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
- byte[] byteArray = concatenate(input);
- String[] output = split(byteArray);
- for (String str: output) {
- System.out.println(str);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement