CLASS zcl_ags_xstream DEFINITION
  PUBLIC
  CREATE PUBLIC .

  PUBLIC SECTION.

    METHODS append
      IMPORTING
        !iv_value TYPE xsequence .
    METHODS append_band01
      IMPORTING
        !iv_value TYPE xsequence .
    METHODS append_band02
      IMPORTING
        !iv_value TYPE xsequence .
    METHODS append_length
      IMPORTING
        !iv_value        TYPE xsequence
      RETURNING
        VALUE(ro_stream) TYPE REF TO zcl_ags_xstream .
    METHODS clear .
    METHODS get
      RETURNING
        VALUE(rv_xstr) TYPE xstring .
    CLASS-METHODS create
      RETURNING
        VALUE(ro_stream) TYPE REF TO zcl_ags_xstream .
  PROTECTED SECTION.
  PRIVATE SECTION.

    DATA mv_xstr TYPE xstring .
ENDCLASS.



CLASS ZCL_AGS_XSTREAM IMPLEMENTATION.


  METHOD append.

    CONCATENATE mv_xstr iv_value INTO mv_xstr IN BYTE MODE.

  ENDMETHOD.


  METHOD append_band01.

    CONSTANTS: lc_band01 TYPE x VALUE '01'.

    DATA: lv_length TYPE xstring.

* make sure to include the length encoding itself and band identifier in the length
    lv_length = zcl_ags_length=>encode( xstrlen( iv_value ) + 5 ).
    append( lv_length ).
    append( lc_band01 ).
    append( iv_value ).

  ENDMETHOD.


  METHOD append_band02.

    CONSTANTS: lc_band02 TYPE x VALUE '02'.

    DATA: lv_length TYPE xstring.

* make sure to include the length encoding itself and band identifier in the length
    lv_length = zcl_ags_length=>encode( xstrlen( iv_value ) + 5 ).
    append( lv_length ).
    append( lc_band02 ).
    append( iv_value ).

  ENDMETHOD.


  METHOD append_length.

    DATA: lv_length TYPE xstring.

    lv_length = zcl_ags_length=>encode( xstrlen( iv_value ) + 4 ).
    append( lv_length ).
    append( iv_value ).

    ro_stream = me.

  ENDMETHOD.


  METHOD clear.

    CLEAR mv_xstr.

  ENDMETHOD.


  METHOD create.

    CREATE OBJECT ro_stream.

  ENDMETHOD.


  METHOD get.
    rv_xstr = mv_xstr.
  ENDMETHOD.
ENDCLASS.
