Skip to content

Long strings are truncated on packing on Android 4.x #406

@paddybyers

Description

@paddybyers

The code below is an Android app that packs and then unpacks a map containing a single String member ("data"). On Android 4.4.2 this member is truncated when packing if it is 512 characters or longer; 511 or fewer and it works fine.

The problem does not occur on Android 5.x or 6.x.

package my.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.msgpack.core.MessageFormat;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessagePacker;
import org.msgpack.core.MessageUnpacker;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		try {
			TestClass testMessage = new TestClass();
			testMessage.data = testString;

			byte[] encoded = writeMsgpack(testMessage);
			TestClass decoded = readMsgpack(encoded);

			String received = decoded.data;
			if(received.equals(testString)) {
				Toast.makeText(this, "Test success", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "Test fail", Toast.LENGTH_LONG).show();
				Log.w(TAG, "Decoded string data: " + received);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

	static byte[] writeMsgpack(TestClass message) {
		byte[] result = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			MessagePacker packer = MessagePack.newDefaultPacker(out);
			message.writeMsgpack(packer);
			packer.flush();
			result = out.toByteArray();
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
		return result;
	}

	static TestClass readMsgpack(byte[] packed) {
		TestClass result = null;
		try {
			MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(packed);
			result = (new TestClass()).readMsgpack(unpacker);
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
		return result;
	}

	static class TestClass {
		public String data;

		void writeMsgpack(MessagePacker packer) throws IOException {
			packer.packMapHeader(1);
			packer.packString("data");
			packer.packString(data);
		}

		TestClass readMsgpack(MessageUnpacker unpacker) throws IOException {
			unpacker.unpackMapHeader();
			String fieldName = unpacker.unpackString();
			MessageFormat fieldFormat = unpacker.getNextFormat();
			if(fieldFormat.equals(MessageFormat.NIL)) {
				unpacker.unpackNil();
			} else if(fieldName.equals("data")) {
				data = unpacker.unpackString();
			}
			return this;
		}
	}

//	/* succeeds */
//	private static String testString = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";

	/* fails */
 	private static String testString = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901";

	private static final String TAG = "MainActivity";
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions