Class Message
- java.lang.Object
-
- org.bitcoinj.core.Message
-
- Direct Known Subclasses:
AddressMessage
,Block
,BloomFilter
,ChildMessage
,EmptyMessage
,FeeFilterMessage
,FilteredBlock
,GetBlocksMessage
,GetUTXOsMessage
,HeadersMessage
,ListMessage
,MemoryPoolMessage
,PartialMerkleTree
,Ping
,Pong
,RejectMessage
,UTXOsMessage
,VersionMessage
public abstract class Message extends java.lang.Object
A Message is a data structure that can be serialized/deserialized using the Bitcoin serialization format. Specific types of messages that are used both in the block chain, and on the wire, are derived from this class.
Instances of this class are not safe for use by multiple threads.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
cursor
protected int
length
static int
MAX_SIZE
protected int
offset
protected NetworkParameters
params
protected byte[]
payload
protected boolean
recached
protected MessageSerializer
serializer
static int
UNKNOWN_LENGTH
-
Constructor Summary
Constructors Modifier Constructor Description protected
Message()
protected
Message(NetworkParameters params)
protected
Message(NetworkParameters params, byte[] payload, int offset)
protected
Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
adjustLength(int newArraySize, int adjustment)
byte[]
bitcoinSerialize()
Returns a copy of the array returned byunsafeBitcoinSerialize()
, which is safe to mutate.void
bitcoinSerialize(java.io.OutputStream stream)
Serialize this message to the provided OutputStream using the bitcoin wire format.protected void
bitcoinSerializeToStream(java.io.OutputStream stream)
Serializes this message to the provided stream.Sha256Hash
getHash()
This method is a NOP for all classes except Block and Transaction.int
getMessageSize()
This returns a correct value by parsing the message.NetworkParameters
getParams()
Network parameters this message was created with.protected boolean
hasMoreBytes()
boolean
isCached()
used for unit testingboolean
isRecached()
protected abstract void
parse()
protected byte
readByte()
protected byte[]
readByteArray()
protected byte[]
readBytes(int length)
protected Sha256Hash
readHash()
protected long
readInt64()
protected java.lang.String
readStr()
protected long
readUint32()
protected java.math.BigInteger
readUint64()
protected VarInt
readVarInt()
protected VarInt
readVarInt(int offset)
void
setSerializer(MessageSerializer serializer)
Overrides the message serializer.protected void
unCache()
To be called before any change of internal values including any setters.byte[]
unsafeBitcoinSerialize()
Serialize this message to a byte array that conforms to the bitcoin wire protocol.
-
-
-
Field Detail
-
MAX_SIZE
public static final int MAX_SIZE
- See Also:
- Constant Field Values
-
UNKNOWN_LENGTH
public static final int UNKNOWN_LENGTH
- See Also:
- Constant Field Values
-
offset
protected int offset
-
cursor
protected int cursor
-
length
protected int length
-
payload
protected byte[] payload
-
recached
protected boolean recached
-
serializer
protected MessageSerializer serializer
-
params
protected NetworkParameters params
-
-
Constructor Detail
-
Message
protected Message()
-
Message
protected Message(NetworkParameters params)
-
Message
protected Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException
- Parameters:
params
- NetworkParameters object.payload
- Bitcoin protocol formatted byte array containing message content.offset
- The location of the first payload byte within the array.serializer
- the serializer to use for this message.length
- The length of message payload if known. Usually this is provided when deserializing of the wire as the length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH- Throws:
ProtocolException
-
Message
protected Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException
- Throws:
ProtocolException
-
-
Method Detail
-
parse
protected abstract void parse() throws ProtocolException
- Throws:
ProtocolException
-
unCache
protected void unCache()
To be called before any change of internal values including any setters. This ensures any cached byte array is removed.
Child messages of this object(e.g. Transactions belonging to a Block) will not have their internal byte caches invalidated unless they are also modified internally.
-
adjustLength
protected void adjustLength(int newArraySize, int adjustment)
-
isCached
public boolean isCached()
used for unit testing
-
isRecached
public boolean isRecached()
-
setSerializer
public void setSerializer(MessageSerializer serializer)
Overrides the message serializer.- Parameters:
serializer
- the new serializer
-
bitcoinSerialize
public byte[] bitcoinSerialize()
Returns a copy of the array returned byunsafeBitcoinSerialize()
, which is safe to mutate. If you need extra performance and can guarantee you won't write to the array, you can use the unsafe version.- Returns:
- a freshly allocated serialized byte array
-
unsafeBitcoinSerialize
public byte[] unsafeBitcoinSerialize()
Serialize this message to a byte array that conforms to the bitcoin wire protocol.
This method may return the original byte array used to construct this message if the following conditions are met:
- 1) The message was parsed from a byte array with parseRetain = true
- 2) The message has not been modified
- 3) The array had an offset of 0 and no surplus bytes
If condition 3 is not met then an copy of the relevant portion of the array will be returned. Otherwise a full serialize will occur. For this reason you should only use this API if you can guarantee you will treat the resulting array as read only.
- Returns:
- a byte array owned by this object, do NOT mutate it.
-
bitcoinSerialize
public final void bitcoinSerialize(java.io.OutputStream stream) throws java.io.IOException
Serialize this message to the provided OutputStream using the bitcoin wire format.- Parameters:
stream
-- Throws:
java.io.IOException
-
bitcoinSerializeToStream
protected void bitcoinSerializeToStream(java.io.OutputStream stream) throws java.io.IOException
Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize().- Throws:
java.io.IOException
-
getHash
public Sha256Hash getHash()
This method is a NOP for all classes except Block and Transaction. It is only declared in Message so BitcoinSerializer can avoid 2 instanceof checks + a casting.
-
getMessageSize
public final int getMessageSize()
This returns a correct value by parsing the message.
-
readUint32
protected long readUint32() throws ProtocolException
- Throws:
ProtocolException
-
readInt64
protected long readInt64() throws ProtocolException
- Throws:
ProtocolException
-
readUint64
protected java.math.BigInteger readUint64() throws ProtocolException
- Throws:
ProtocolException
-
readVarInt
protected VarInt readVarInt() throws ProtocolException
- Throws:
ProtocolException
-
readVarInt
protected VarInt readVarInt(int offset) throws ProtocolException
- Throws:
ProtocolException
-
readBytes
protected byte[] readBytes(int length) throws ProtocolException
- Throws:
ProtocolException
-
readByte
protected byte readByte() throws ProtocolException
- Throws:
ProtocolException
-
readByteArray
protected byte[] readByteArray() throws ProtocolException
- Throws:
ProtocolException
-
readStr
protected java.lang.String readStr() throws ProtocolException
- Throws:
ProtocolException
-
readHash
protected Sha256Hash readHash() throws ProtocolException
- Throws:
ProtocolException
-
hasMoreBytes
protected boolean hasMoreBytes()
-
getParams
public NetworkParameters getParams()
Network parameters this message was created with.
-
-