1 var DATA_TYPES = require('../../constants/DataTypes'),
  2   Helpers = require('../../utils/Helpers'),
  3   ErrorMessages = require('../../constants/ErrorMessages'),
  4   CAS = require('../../constants/CASConstants');
  5 
  6 module.exports = LOBReadPacket;
  7 
  8 /**
  9  * Constructor
 10  * @param options
 11  * @constructor
 12  */
 13 function LOBReadPacket(options) {
 14   this.casInfo = options.casInfo;
 15 
 16   this.responseCode = 0;
 17   this.errorCode = 0;
 18   this.errorMsg = '';
 19   this.lobBuffer = '';
 20   this.lobHandle = '';
 21 }
 22 
 23 /**
 24  * Write data
 25  * @param writer
 26  * @param lobHandle
 27  * @param offset
 28  * @param len
 29  */
 30 LOBReadPacket.prototype.write = function (writer, lobHandle, offset, len) {
 31   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 32     DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + lobHandle.packedLobHandle.length +
 33     DATA_TYPES.INT_SIZEOF + DATA_TYPES.LONG_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF;
 34 
 35   this.lobHandle = lobHandle;
 36   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 37   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 38 
 39   writer._writeByte(CAS.CASFunctionCode.CAS_FC_LOB_READ);
 40   writer._writeInt(lobHandle.packedLobHandle.length); //LOB handle size
 41   writer._writeBytes(lobHandle.packedLobHandle.length, lobHandle.packedLobHandle); //LOB handle
 42   writer._writeInt(DATA_TYPES.LONG_SIZEOF);
 43   writer._writeLong(offset); //ofset
 44   writer._writeInt(DATA_TYPES.INT_SIZEOF);
 45   writer._writeInt(len); //number of bytes to read
 46 
 47   return writer;
 48 };
 49 
 50 /**
 51  * Read data
 52  * @param parser
 53  */
 54 LOBReadPacket.prototype.parse = function (parser) {
 55   var responseLength = parser._parseInt();
 56   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
 57 
 58   this.responseCode = parser._parseInt();
 59   if (this.responseCode < 0) {
 60     this.errorCode = parser._parseInt();
 61     this.errorMsg = parser._parseNullTerminatedString(responseLength - 2 * DATA_TYPES.INT_SIZEOF);
 62     if (this.errorMsg.length == 0) {
 63       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
 64     }
 65   } else {
 66     if (this.lobHandle.lobType == CAS.CUBRIDDataType.CCI_U_TYPE_BLOB) {
 67       this.lobBuffer = parser._parseBytes(this.responseCode);
 68     } else {
 69       this.lobBuffer = parser._parseString(this.responseCode);
 70     }
 71     return this.responseCode;
 72   }
 73 };
 74 
 75 
 76