简体   繁体   English

如何从Java中的字节数组获取数据类型

[英]How to get Data type from Byte Array in Java

I have a Java app that lets users to store the data in database but while storing I store those data as byte array which is same as cassandra, Now when I get back the byte array I want to convert those data as User saved, means if user saved as Long I want to show long value, or if User saved String I want to show String value. 我有一个Java应用程序,允许用户将数据存储在数据库中,但是在存储时,我将这些数据存储为与cassandra相同的字节数组,现在当我取回字节数组时,我想将这些数据转换为用户保存的,这意味着用户另存为Long我想显示long值,或者如果用户保存String我想显示String值。 Now If I converts all the bytes array fields to string, apparently long bytes array will be shown as wild char.string fields would be fine. 现在,如果我将所有的bytes数组字段都转换为string,显然长字节数组将显示为wild char.string字段就可以了。

Please suggest me how to solve this issue in java. 请建议我如何用Java解决此问题。 Its similar to cassandra way of storing data.cassandra store all the data as byte array. 它类似于cassandra的数据存储方式。cassandra将所有数据存储为字节数组。

Basically I want to know the datatype of the byte array. 基本上我想知道字节数组的数据类型。

Your question isn't very clear as to what exactly you want but... 关于您到底想要什么,您的问题不是很清楚,但是...

You could come up with some custom scheme for doing this like the first byte of the array indicates what type and the remaining bytes are the actual data. 您可以提出一些自定义方案来执行此操作,例如数组的第一个字节指示什么类型,其余字节为实际数据。 You then need to write code to convert the byte[1] through byte[length-1] into that given type. 然后,您需要编写代码以将byte [1]到byte [length-1]转换为该给定类型。 It seems like a lot of work to me. 对我来说似乎是很多工作。

I would probably try using object serialization. 我可能会尝试使用对象序列化。 It basically does what you are asking here w/o any custom code on your end. 它基本上可以满足您的要求,而无需使用任何自定义代码。

public static void main(String[] args) throws Exception {
    String strValue = "hello";
    int myInt = 3;
    long myLong = 45677;
    short myShort = 1;
    double myFloat = 4.5;

    serializeThenDeserialize(strValue);
    serializeThenDeserialize(myInt);
    serializeThenDeserialize(myLong);
    serializeThenDeserialize(myShort);
    serializeThenDeserialize(myFloat);
}

private static void serializeThenDeserialize(Object value) throws Exception {
    System.out.println("Input Type is " + value.getClass() + " with value '" + value + "'");
    ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteArrayStream);
    out.writeObject(value);
    out.close();

    byte[] objectAsBytes = byteArrayStream.toByteArray();
    // Persist here..


    // Now lets deserialize the byte array
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objectAsBytes));
    Object deserializedValue = in.readObject();
    in.close();

    System.out.println("Deserialized Type is " + deserializedValue.getClass() + " with Value '" + deserializedValue + "'");
    System.out.println();
}

When running this it acts like we want. 运行此程序时,它的作用就像我们想要的。 Data is returned and type is maintained. 返回数据并维护类型。

Input Type is class java.lang.String with value 'hello'
Deserialized Type is class java.lang.String with Value 'hello'

Input Type is class java.lang.Integer with value '3'
Deserialized Type is class java.lang.Integer with Value '3'

Input Type is class java.lang.Long with value '45677'
Deserialized Type is class java.lang.Long with Value '45677'

Input Type is class java.lang.Short with value '1'
Deserialized Type is class java.lang.Short with Value '1'

Input Type is class java.lang.Double with value '4.5'
Deserialized Type is class java.lang.Double with Value '4.5'

The good thing about this is that it works for all Java objects. 这样做的好处是它适用于所有Java对象。 The bad thing is that Java object serialization can get a little harry as the objects you are storing evolve (ie you remove methods, field, compile w/ different JDK, etc). 不好的事情是,随着存储对象的发展,Java对象序列化会变得有些困难(即,删除方法,字段,使用不同的JDK进行编译等)。 If you stick to primitives you should have no problems w/ this though. 如果您坚持使用原语,那么应该不会有任何问题。 If you serialize your own objects you should read more on compatible and incompatible changes here . 如果序列化自己的对象,则应在此处阅读有关兼容和不兼容更改的更多信息

I'd recommend to serialize data in some format, that stores type info, like BSON: http://bsonspec.org/ or Smile: http://wiki.fasterxml.com/SmileFormat 我建议以某种格式序列化存储类型信息的数据,例如BSON: http//bsonspec.org/或Smile: http ://wiki.fasterxml.com/SmileFormat

In this case, deserialization will restore type info, and after deserialization you'll get Object of correct type. 在这种情况下,反序列化将恢复类型信息,反序列化后,您将获得正确类型的Object。

These formats are very compact: type info takes only a few extra bytes, as opposed to java standart serialization, that requires several hundreeds bytes to serialize the simplest object. 这些格式非常紧凑:与Java标准序列化相反,类型信息仅占用几个额外的字节,而Java标准序列化需要几个hundreeds字节来序列化最简单的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM