Akka Study Notes (3) -- bytestring

Source: Internet
Author: User

For the io layer design of akka, refer to this document. This article briefly introduces the design of bytestring.

Immutable message

The actor communicates with each other through messages, but to avoid synchronization problems, messages must beImmutable. Therefore, akka cannot be used.Byte []OrBytebufferBut designedBytestringTo represent binary data. It is important to understand this because bytestring is immutable. Therefore, many methods that seem to modify the status of bytestring actually return a new bytestring instance. IfStringOrBigintegerThis can be easily understood if you are familiar with the non-variable classes that come with Java.

Rope Data Structure

Bytestring is a data structure similar to rope, as shown in:

Although the internal bytestring uses a tree structure to store N small bytes [], from the external point of view, bytestring is still like a large byte [].

Factory method

Bytestring isAbstract classAnd cannot be directly instantiated. AvailableFactory methodTo create a bytestring instance. bytestring provides six factory methods, as shown below:

public static ByteString empty()public static ByteString fromArray(byte[] array)public static ByteString fromArray(byte[] array, int offset, int length)public static ByteString fromString(String string)public static ByteString fromString(String string, String charset)public static ByteString fromByteBuffer(ByteBuffer buffer)
The empty () method returns an empty bytestring. Other methods can create bytestring instances based on byte [], string, or bytebuffer. It should be noted that, in order to ensure that the status cannot be changed, the factory method may perform data Copy.

Common Methods

Below are some commonly used bytestring methods (I use a similar string method to explain ):

  • Public int size ()//String. Length ()
  • Public bytestring Concat (bytestring BS)// String. Concat (STR)
  • Public byte head ()// String. charat (0)
  • Public bytestring tail ()// String. substring (1, String. Length ())
  • Public bytestring take (int n)//String. charat (N)
  • Public bytestring drop (int n)// String. substring (n, String. Length ())
  • Public bytestring slice (int from, int )//String. substring (from,)
Bytestringbuilder

We all know that the stringbuilder class (or its thread-safe version stringbuffer) should be used to generate a long string ). For the same purpose, akka providesBytestringbuilderTo create a bytestring. The following is an example of bytestringbuilder usage:

ByteStringBuilder bsb = new ByteStringBuilder();bsb.append(ByteString.fromString("abc"));bsb.putByte((byte) 1);bsb.putInt(32, ByteOrder.BIG_ENDIAN);bsb.putDouble(3.14, ByteOrder.BIG_ENDIAN);bsb.putBytes(new byte[] {1, 2, 3});ByteString bs = bsb.result();

Byteiterator

To facilitate access to data in bytestring, akka providesByteiteratorClass. You can use bytestring'sIterator ()Method to obtain a byteiterator instance. The following is an example of byteiterator usage:

ByteIterator it = bs.iterator();it.getByte();it.getInt(ByteOrder.BIG_ENDIAN);it.getDouble(ByteOrder.BIG_ENDIAN);it.getBytes(new byte[10]);

Interoperability with Java. Io

Call bytestringbuilder'sAsoutputstream ()You can use bytestringbuilder as outputstream. Call byteiterator'sAsinputstream ()You can use byteiterator as inputstream.

Conclusion

Bytestring is similar to string in terms of immutable (I guess that's why it is namedBytestring). Remember this when using bytestring.

Akka Study Notes (3) -- bytestring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.