镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

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";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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