bool ConnectionReader::readFromStream(PortReader& portable, InputStream& is) { StreamConnectionReader reader; Route r; reader.reset(is, nullptr, r, 0, false); return portable.read(reader); }
ConnectionReader *ConnectionReader::createConnectionReader(InputStream& is) { StreamConnectionReader *reader = new StreamConnectionReader(); Route r; reader->reset(is, nullptr, r, 0, false); return reader; }
void BottleImpl::fromBinary(const char *text, int len) { String wrapper(text,len); StringInputStream sis; sis.add(wrapper); StreamConnectionReader reader; Route route; reader.reset(sis,NULL,route,len,false); read(reader); }
void testRead() { report(0,"testing reading..."); StringInputStream sis; StringOutputStream sos; sis.add("Hello\ngood evening and welcome"); StreamConnectionReader sbr; Route route; sbr.reset(sis,NULL,route,10,true); String line = sbr.expectLine(); checkEqual(line,"Hello","one line"); }
void testTextReading() { report(0,"testing text-mode reading..."); PortCommand cmd; StringInputStream sis; StreamConnectionReader br; sis.add("d\r\n"); Route route; br.reset(sis,NULL,route,sis.toString().length(),true); cmd.read(br); checkEqual('d',cmd.getKey(),"basic data command"); }
bool getEnvelope(PortReader& envelope) { if (prev==nullptr) { return false; } StringInputStream sis; sis.add(prev->envelope.c_str()); sis.add("\r\n"); StreamConnectionReader sbr; Route route; sbr.reset(sis, nullptr, route, 0, true); return envelope.read(sbr); }
bool BottleImpl::fromBytes(const Bytes& data) { String wrapper(data.get(),data.length()); StringInputStream sis; sis.add(wrapper); StreamConnectionReader reader; Route route; reader.reset(sis,NULL,route,data.length(),false); clear(); dirty = true; // for clarity if (!nested) { clear(); specialize(0); int code = reader.expectInt(); if (reader.isError()) { return false; } YMSG(("READ got top level code %d\n", code)); code = code & UNIT_MASK; if (code!=0) { specialize(code); } } int len = reader.expectInt(); if (reader.isError()) { return false; } YMSG(("READ bottle length %d\n", len)); for (int i=0; i<len; i++) { bool ok = fromBytes(reader); if (!ok) return false; } return true; }
void testReread() { report(0,"testing reread specialization is not broken..."); Bottle bot("10 20 30"); // first a specialized list Bottle bot2; { BufferedConnectionWriter writer(false); bot.write(writer); StringInputStream sis; sis.add(writer.toString()); StreamConnectionReader br; br.reset(sis,NULL,Route(),sis.toString().length(),false); bot2.read(br); //printf("bot is %s\n", bot.toString().c_str()); //printf("bot2 is %s\n", bot2.toString().c_str()); checkEqual(bot.size(),bot2.size(),"length check"); checkTrue(bot2.get(2).isInt(),"type check"); checkEqual(bot.toString().c_str(),bot2.toString().c_str(), "content check"); } bot.fromString("10 20 30.5"); // now an unspecialized list { BufferedConnectionWriter writer(false); bot.write(writer); StringInputStream sis; sis.add(writer.toString()); StreamConnectionReader br; br.reset(sis,NULL,Route(),sis.toString().length(),false); bot2.read(br); checkEqual(bot.size(),bot2.size(),"length check"); checkTrue(bot2.get(2).isDouble(),"type check"); checkEqual(bot.toString().c_str(),bot2.toString().c_str(), "content check"); } }
void testStreaming() { report(0,"testing streaming (just text mode)..."); BottleImpl bot; bot.addInt(5); bot.addString("hello"); BufferedConnectionWriter bbw(true); bot.write(bbw); String s; StringInputStream sis; StreamConnectionReader sbr; s = bbw.toString(); sis.add(s); Route route; sbr.reset(sis,NULL,route,s.length(),true); BottleImpl bot2; bot2.read(sbr); checkEqual(bot2.toString(),bot.toString(),"to/from stream"); }