public abstract class Message extends 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.
Modifier and Type | Field and Description |
---|---|
protected int |
cursor |
protected int |
length |
static int |
MAX_SIZE |
protected int |
offset |
protected NetworkParameters |
params |
protected byte[] |
payload |
protected int |
protocolVersion |
protected boolean |
recached |
protected MessageSerializer |
serializer |
static int |
UNKNOWN_LENGTH |
Modifier | Constructor and Description |
---|---|
protected |
Message() |
protected |
Message(NetworkParameters params) |
protected |
Message(NetworkParameters params,
byte[] payload,
int offset) |
protected |
Message(NetworkParameters params,
byte[] payload,
int offset,
int protocolVersion) |
protected |
Message(NetworkParameters params,
byte[] payload,
int offset,
int protocolVersion,
MessageSerializer serializer,
int length) |
protected |
Message(NetworkParameters params,
byte[] payload,
int offset,
MessageSerializer serializer,
int length) |
Modifier and Type | Method and Description |
---|---|
protected void |
adjustLength(int newArraySize,
int adjustment) |
byte[] |
bitcoinSerialize()
Returns a copy of the array returned by
unsafeBitcoinSerialize() , which is safe to mutate. |
void |
bitcoinSerialize(OutputStream stream)
Serialize this message to the provided OutputStream using the bitcoin wire format.
|
protected void |
bitcoinSerializeToStream(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 testing
|
boolean |
isRecached() |
protected abstract void |
parse() |
protected byte[] |
readByteArray() |
protected byte[] |
readBytes(int length) |
protected Sha256Hash |
readHash() |
protected long |
readInt64() |
protected String |
readStr() |
protected long |
readUint32() |
protected BigInteger |
readUint64() |
protected long |
readVarInt() |
protected long |
readVarInt(int offset) |
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.
|
public static final int MAX_SIZE
public static final int UNKNOWN_LENGTH
protected int offset
protected int cursor
protected int length
protected byte[] payload
protected boolean recached
protected MessageSerializer serializer
protected int protocolVersion
protected NetworkParameters params
protected Message()
protected Message(NetworkParameters params)
protected Message(NetworkParameters params, byte[] payload, int offset, int protocolVersion) throws ProtocolException
ProtocolException
protected Message(NetworkParameters params, byte[] payload, int offset, int protocolVersion, MessageSerializer serializer, int length) throws ProtocolException
params
- NetworkParameters object.payload
- Bitcoin protocol formatted byte array containing message content.offset
- The location of the first payload byte within the array.protocolVersion
- Bitcoin protocol version.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_LENGTHProtocolException
protected Message(NetworkParameters params, byte[] payload, int offset) throws ProtocolException
ProtocolException
protected Message(NetworkParameters params, byte[] payload, int offset, MessageSerializer serializer, int length) throws ProtocolException
ProtocolException
protected abstract void parse() throws ProtocolException
ProtocolException
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.
protected void adjustLength(int newArraySize, int adjustment)
public boolean isCached()
public boolean isRecached()
public byte[] bitcoinSerialize()
unsafeBitcoinSerialize()
, 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.public byte[] unsafeBitcoinSerialize()
public final void bitcoinSerialize(OutputStream stream) throws IOException
stream
- IOException
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException
IOException
public Sha256Hash getHash()
public final int getMessageSize()
protected long readUint32() throws ProtocolException
ProtocolException
protected long readInt64() throws ProtocolException
ProtocolException
protected BigInteger readUint64() throws ProtocolException
ProtocolException
protected long readVarInt() throws ProtocolException
ProtocolException
protected long readVarInt(int offset) throws ProtocolException
ProtocolException
protected byte[] readBytes(int length) throws ProtocolException
ProtocolException
protected byte[] readByteArray() throws ProtocolException
ProtocolException
protected String readStr() throws ProtocolException
ProtocolException
protected Sha256Hash readHash() throws ProtocolException
ProtocolException
protected boolean hasMoreBytes()
public NetworkParameters getParams()
Copyright © 2016. All rights reserved.