github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/writer/binaryWriter.go (about)

     1  package writer
     2  
     3  import (
     4  	"encoding/binary"
     5  	"io"
     6  )
     7  
     8  // bufferSizeInBytes is the size of the buffer used by BinaryWriter
     9  //
    10  const bufferSizeInBytes = 16
    11  
    12  // BinaryWriter is the writer which can be used to write values of primitive types to a writer
    13  // The writer supports writing data both in little-endian or big-endian format.
    14  //
    15  type BinaryWriter struct {
    16  	buffer []byte
    17  	order  binary.ByteOrder
    18  	to     io.WriterAt
    19  	Size   int64
    20  }
    21  
    22  // NewBinaryWriter creates a new instance of BinaryWriter, to is the underlying data source
    23  // to write to, order is the byte order used to encode the data in the source, size is the
    24  // length of the data source in bytes.
    25  //
    26  func NewBinaryWriter(to io.WriterAt, order binary.ByteOrder, size int64) *BinaryWriter {
    27  	return &BinaryWriter{
    28  		buffer: make([]byte, bufferSizeInBytes),
    29  		order:  order,
    30  		to:     to,
    31  		Size:   size,
    32  	}
    33  }
    34  
    35  // WriteBytes writes a byte slice to the underlying writer at offset off.
    36  //
    37  func (w *BinaryWriter) WriteBytes(off int64, value []byte) {
    38  	w.to.WriteAt(value, off)
    39  }
    40  
    41  // WriteByte write a byte value to the underlying writer at offset off.
    42  //
    43  func (w *BinaryWriter) WriteByte(off int64, value byte) {
    44  	w.buffer[0] = value
    45  	w.to.WriteAt(w.buffer[:1], off)
    46  }
    47  
    48  // WriteBoolean write a boolean value to the underlying writer at offset off.
    49  //
    50  func (w *BinaryWriter) WriteBoolean(off int64, value bool) {
    51  	if value {
    52  		w.buffer[0] = 1
    53  	} else {
    54  		w.buffer[0] = 0
    55  	}
    56  
    57  	w.to.WriteAt(w.buffer[:1], off)
    58  }
    59  
    60  // WriteInt16 encodes an int16 and write it in the underlying writer at offset off.
    61  //
    62  func (w *BinaryWriter) WriteInt16(off int64, value int16) {
    63  	w.order.PutUint16(w.buffer, uint16(value))
    64  	w.to.WriteAt(w.buffer[:2], off)
    65  }
    66  
    67  // WriteUInt16 encodes an uint16 and write it in the underlying writer at offset off.
    68  //
    69  func (w *BinaryWriter) WriteUInt16(off int64, value uint16) {
    70  	w.order.PutUint16(w.buffer, value)
    71  	w.to.WriteAt(w.buffer[:2], off)
    72  }
    73  
    74  // WriteInt32 encodes an int32 and write it in the underlying writer at offset off.
    75  //
    76  func (w *BinaryWriter) WriteInt32(off int64, value int32) {
    77  	w.order.PutUint32(w.buffer, uint32(value))
    78  	w.to.WriteAt(w.buffer[:4], off)
    79  }
    80  
    81  // WriteUInt32 encodes an uint32 and write it in the underlying writer at offset off.
    82  //
    83  func (w *BinaryWriter) WriteUInt32(off int64, value uint32) {
    84  	w.order.PutUint32(w.buffer, value)
    85  	w.to.WriteAt(w.buffer[:4], off)
    86  }
    87  
    88  // WriteInt64 encodes an int64 and write it in the underlying writer at offset off.
    89  //
    90  func (w *BinaryWriter) WriteInt64(off int64, value int64) {
    91  	w.order.PutUint64(w.buffer, uint64(value))
    92  	w.to.WriteAt(w.buffer[:8], off)
    93  }
    94  
    95  // WriteUInt64 encodes an uint64 and write it in the underlying writer at offset off.
    96  //
    97  func (w *BinaryWriter) WriteUInt64(off int64, value uint64) {
    98  	w.order.PutUint64(w.buffer, value)
    99  	w.to.WriteAt(w.buffer[:8], off)
   100  }
   101  
   102  // WriteString writes a string to the underlying writer at offset off.
   103  //
   104  func (w *BinaryWriter) WriteString(off int64, value string) {
   105  	w.to.WriteAt([]byte(value), off)
   106  }