result_t Buffer::append(const char *str, const char *codec) { if (!qstricmp(codec, "utf8") || !qstricmp(codec, "utf-8")) { size_t sz = qstrlen(str); extMemory((int32_t) sz); m_data.append(str, sz); return 0; } obj_ptr<Buffer_base> data; result_t hr; if (!qstrcmp(codec, "hex")) hr = hex_base::decode(str, data); else if (!qstrcmp(codec, "base64")) hr = base64_base::decode(str, data); else hr = iconv_base::encode(codec, str, data); if (hr < 0) return hr; return append(data); }
result_t Buffer::write(v8::Local<v8::Array> datas) { int32_t sz = datas->Length(); if (sz) { int32_t i; result_t hr; std::string str; str.resize(sz); for (i = 0; i < sz; i ++) { v8::Local<v8::Value> v = datas->Get(i); int32_t num; hr = GetArgumentValue(v, num); if (hr < 0) return hr; if (num < 0 || num > 256) return CHECK_ERROR(CALL_E_OUTRANGE); str[i] = num; } extMemory((int) sz); m_data.append(str); } return 0; }
result_t Buffer::write(const char *str, const char *codec) { if (!qstricmp(codec, "utf8") || !qstricmp(codec, "utf-8")) { size_t sz = qstrlen(str); extMemory((int) sz); m_data.append(str, sz); return 0; } obj_ptr<Buffer_base> data; result_t hr; if (!qstrcmp(codec, "hex")) hr = encoding_base::hexDecode(str, data); else if (!qstrcmp(codec, "base64")) hr = encoding_base::base64Decode(str, data); else hr = encoding_base::iconvEncode(codec, str, data); if (hr < 0) return hr; return write(data); }
result_t Buffer::append(Buffer_base *data) { std::string strBuf; data->toString(strBuf); extMemory((int32_t) strBuf.length()); m_data.append(strBuf); return 0; }
result_t Buffer::resize(int32_t sz) { if (sz < 0) return CHECK_ERROR(CALL_E_INVALIDARG); extMemory(sz - (int32_t) m_data.length()); m_data.resize(sz); return 0; }
result_t Buffer::append(v8::Local<v8::ArrayBuffer> datas) { v8::ArrayBuffer::Contents cnt = datas->GetContents(); int32_t sz = (int32_t)cnt.ByteLength(); char* ptr = (char*)cnt.Data(); if (sz) { extMemory(sz); m_data.append(ptr, sz); } return 0; }
result_t MemoryStream::write(Buffer_base *data, AsyncEvent *ac) { exlib::string strBuf; int64_t sz1, sz2; data->toString(strBuf); size(sz1); m_buffer.write(strBuf.c_str(), (int32_t) strBuf.length()); m_buffer.seekg(m_buffer.tellp()); size(sz2); if (sz2 > sz1) extMemory((int32_t) (sz2 - sz1)); m_time.now(); return 0; }