Package org.bitcoinj.core
Class BitcoinSerializer
- java.lang.Object
-
- org.bitcoinj.core.MessageSerializer
-
- org.bitcoinj.core.BitcoinSerializer
-
public class BitcoinSerializer extends MessageSerializer
Methods to serialize and de-serialize messages to the Bitcoin network format as defined in the protocol specification.
To be able to serialize and deserialize new Message subclasses the following criteria needs to be met.
- The proper Class instance needs to be mapped to its message name in the names variable below
- There needs to be a constructor matching: NetworkParameters params, byte[] payload
- Message.bitcoinSerializeToStream() needs to be properly subclassed
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBitcoinSerializer.BitcoinPacketHeader
-
Constructor Summary
Constructors Constructor Description BitcoinSerializer(NetworkParameters params, boolean parseRetain)Constructs a BitcoinSerializer with the given behavior.BitcoinSerializer(NetworkParameters params, int protocolVersion, boolean parseRetain)Constructs a BitcoinSerializer with the given behavior.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Messagedeserialize(java.nio.ByteBuffer in)Reads a message from the given ByteBuffer and returns it.BitcoinSerializer.BitcoinPacketHeaderdeserializeHeader(java.nio.ByteBuffer in)Deserializes only the header in case packet meta data is needed before decoding the payload.MessagedeserializePayload(BitcoinSerializer.BitcoinPacketHeader header, java.nio.ByteBuffer in)Deserialize payload only.booleanequals(java.lang.Object o)NetworkParametersgetParameters()Get the network parameters for this serializer.intgetProtocolVersion()Get the protocol version of this serializer.inthashCode()booleanisParseRetainMode()Whether the serializer will produce cached mode MessagesAddressV1MessagemakeAddressV1Message(byte[] payloadBytes, int length)Make an address message from the payload.AddressV2MessagemakeAddressV2Message(byte[] payloadBytes, int length)Make an address message from the payload.BlockmakeBlock(byte[] payloadBytes, int offset, int length)Make a block from the payload.MessagemakeBloomFilter(byte[] payloadBytes)Make an filter message from the payload.FilteredBlockmakeFilteredBlock(byte[] payloadBytes)Make a filtered block from the payload.InventoryMessagemakeInventoryMessage(byte[] payloadBytes, int length)Make an inventory message from the payload.TransactionmakeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader)Make a transaction from the payload.voidseekPastMagicBytes(java.nio.ByteBuffer in)voidserialize(java.lang.String name, byte[] message, java.io.OutputStream out)Writes message to to the output stream.voidserialize(Message message, java.io.OutputStream out)Writes message to to the output stream.BitcoinSerializerwithProtocolVersion(int protocolVersion)Create a new serializer with a specific protocol version.-
Methods inherited from class org.bitcoinj.core.MessageSerializer
makeBlock, makeBlock, makeTransaction, makeTransaction
-
-
-
-
Constructor Detail
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params- networkParams used to create Messages instances and determining packetMagicparseRetain- retain the backing byte array of a message for fast reserialization.
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params, int protocolVersion, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params- networkParams used to create Messages instances and determining packetMagicprotocolVersion- the protocol version to useparseRetain- retain the backing byte array of a message for fast reserialization.
-
-
Method Detail
-
withProtocolVersion
public BitcoinSerializer withProtocolVersion(int protocolVersion)
Description copied from class:MessageSerializerCreate a new serializer with a specific protocol version. Mainly used to disable SegWit when parsing transactions.- Specified by:
withProtocolVersionin classMessageSerializer
-
getProtocolVersion
public int getProtocolVersion()
Description copied from class:MessageSerializerGet the protocol version of this serializer.- Specified by:
getProtocolVersionin classMessageSerializer
-
serialize
public void serialize(java.lang.String name, byte[] message, java.io.OutputStream out) throws java.io.IOExceptionWrites message to to the output stream.- Specified by:
serializein classMessageSerializer- Throws:
java.io.IOException
-
serialize
public void serialize(Message message, java.io.OutputStream out) throws java.io.IOException
Writes message to to the output stream.- Specified by:
serializein classMessageSerializer- Throws:
java.io.IOException
-
deserialize
public Message deserialize(java.nio.ByteBuffer in) throws ProtocolException, java.io.IOException
Reads a message from the given ByteBuffer and returns it.- Specified by:
deserializein classMessageSerializer- Throws:
ProtocolExceptionjava.io.IOException
-
deserializeHeader
public BitcoinSerializer.BitcoinPacketHeader deserializeHeader(java.nio.ByteBuffer in) throws ProtocolException, java.io.IOException
Deserializes only the header in case packet meta data is needed before decoding the payload. This method assumes you have already called seekPastMagicBytes()- Specified by:
deserializeHeaderin classMessageSerializer- Throws:
ProtocolExceptionjava.io.IOException
-
deserializePayload
public Message deserializePayload(BitcoinSerializer.BitcoinPacketHeader header, java.nio.ByteBuffer in) throws ProtocolException, java.nio.BufferUnderflowException
Deserialize payload only. You must provide a header, typically obtained by callingdeserializeHeader(java.nio.ByteBuffer).- Specified by:
deserializePayloadin classMessageSerializer- Throws:
ProtocolExceptionjava.nio.BufferUnderflowException
-
getParameters
public NetworkParameters getParameters()
Get the network parameters for this serializer.
-
makeAddressV1Message
public AddressV1Message makeAddressV1Message(byte[] payloadBytes, int length) throws ProtocolException
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV1Messagein classMessageSerializer- Throws:
ProtocolException
-
makeAddressV2Message
public AddressV2Message makeAddressV2Message(byte[] payloadBytes, int length) throws ProtocolException
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV2Messagein classMessageSerializer- Throws:
ProtocolException
-
makeBlock
public Block makeBlock(byte[] payloadBytes, int offset, int length) throws ProtocolException
Make a block from the payload. Extension point for alternative serialization format support.- Specified by:
makeBlockin classMessageSerializer- Throws:
ProtocolException
-
makeBloomFilter
public Message makeBloomFilter(byte[] payloadBytes) throws ProtocolException
Make an filter message from the payload. Extension point for alternative serialization format support.- Specified by:
makeBloomFilterin classMessageSerializer- Throws:
ProtocolException
-
makeFilteredBlock
public FilteredBlock makeFilteredBlock(byte[] payloadBytes) throws ProtocolException
Make a filtered block from the payload. Extension point for alternative serialization format support.- Specified by:
makeFilteredBlockin classMessageSerializer- Throws:
ProtocolException
-
makeInventoryMessage
public InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) throws ProtocolException
Make an inventory message from the payload. Extension point for alternative serialization format support.- Specified by:
makeInventoryMessagein classMessageSerializer- Throws:
ProtocolException
-
makeTransaction
public Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader) throws ProtocolException
Make a transaction from the payload. Extension point for alternative serialization format support.- Specified by:
makeTransactionin classMessageSerializer- Throws:
ProtocolException
-
seekPastMagicBytes
public void seekPastMagicBytes(java.nio.ByteBuffer in) throws java.nio.BufferUnderflowException- Specified by:
seekPastMagicBytesin classMessageSerializer- Throws:
java.nio.BufferUnderflowException
-
isParseRetainMode
public boolean isParseRetainMode()
Whether the serializer will produce cached mode Messages- Specified by:
isParseRetainModein classMessageSerializer
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-