github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/object/object.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package object
     4  
     5  import (
     6  	"io"
     7  
     8  	"../compress"
     9  	"../mediatype"
    10  	"../protocol"
    11  	"../syllab"
    12  )
    13  
    14  // Object has needed methods to implements protocol.Object interface!
    15  type Object []byte
    16  
    17  func (ob Object) Metadata() protocol.ObjectMetadata { return Metadata(ob) }
    18  func (ob Object) Data() protocol.Codec              { return ob[MetadataLength:] }
    19  
    20  func NewObject(data protocol.Codec) Object { return make([]byte, 0, int(MetadataLength)+data.Len()) }
    21  
    22  /*
    23  ********** protocol.Codec interface **********
    24   */
    25  
    26  func (ob Object) MediaType() protocol.MediaType {
    27  	return mediatype.MediaTypeByID(ob.Metadata().MediaTypeID())
    28  }
    29  func (ob Object) CompressType() protocol.CompressType {
    30  	return compress.CompressTypeByID(ob.Metadata().CompressTypeID())
    31  }
    32  
    33  func (ob Object) Decode(reader io.Reader) (err protocol.Error) { err = ErrSourceNotChangeable; return }
    34  func (ob Object) Encode(writer io.Writer) (err error)          { _, err = ob.WriteTo(writer); return }
    35  func (ob Object) Unmarshal(data []byte) (err protocol.Error)   { err = ErrSourceNotChangeable; return }
    36  func (ob Object) Marshal() (data []byte)                       { return ob }
    37  func (ob Object) MarshalTo(data []byte) []byte                 { return append(data, ob...) }
    38  func (ob Object) Len() int                                     { return len(ob) }
    39  
    40  /*
    41  ********** io package interfaces **********
    42   */
    43  
    44  func (ob Object) ReadFrom(reader io.Reader) (n int64, err error) {
    45  	err = ErrSourceNotChangeable
    46  	return
    47  }
    48  func (ob Object) WriteTo(writer io.Writer) (n int64, err error) {
    49  	var writeLength int
    50  	writeLength, err = writer.Write(ob)
    51  	n = int64(writeLength)
    52  	return
    53  }
    54  
    55  /*
    56  ********** protocol.Syllab interface **********
    57   */
    58  
    59  func (ob Object) CheckSyllab(payload []byte) (err protocol.Error) {
    60  	if len(payload) < int(ob.LenOfSyllabStack()) {
    61  		err = syllab.ErrShortArrayDecode
    62  	}
    63  	return
    64  }
    65  func (ob Object) FromSyllab(payload []byte, stackIndex uint32) {
    66  	// err = ErrSourceNotChangeable
    67  }
    68  func (ob Object) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
    69  	freeHeapIndex = syllab.SetByteArray(payload, ob, stackIndex, heapIndex)
    70  	return
    71  }
    72  func (ob Object) LenAsSyllab() uint64          { return uint64(ob.LenOfSyllabStack() + ob.LenOfSyllabHeap()) }
    73  func (ob Object) LenOfSyllabStack() uint32     { return 8 }
    74  func (ob Object) LenOfSyllabHeap() (ln uint32) { ln = uint32(len(ob)); return }