Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » NEW: BufferStream
NEW: BufferStream [message #27938] Wed, 11 August 2010 11:25 Go to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi guys,

i was in need of a BufferStream, a stream that can store data inside a Vector<byte>, which i find beeing more convenient than the StringBuffer, which is quite hard to understand Smile (having both a String data and a StringBuffer etc..where data is copied around i think..which is not what i needed). BTW: why was that nessessary, why couldnt it have been only StringBuffer or only String data?

so i tried to use my brandnew Stream knowledge to make a Vector<byte> based buffer, maybe one could take a look on it and spot some conceptional mistakes, if any..(ofcorse there are:)

it should support storing data and GetResult a Vector<byte> back, picking internal one, which then is resetet to be used again. it reserves some more space in advance..which is not equal to currently stored stuff there..so this is definitely fix issue yet.

maybe a i only dont understand the StringBuffer well, maybe it could do it for me as well. but for for now it is my inly option. if somebody can explain the internal concept of StringBuffer, i'd appreciate it.. so here comes the class, attached a Test app.

class BufferStream : public MemStream {
protected:
	virtual  void  _Put(int w) { byte h = w; _Put(&h, 1); }
	virtual  void  _Put(const void *data, dword size)
	{
		if(size > (dword)(uintptr_t)(wrlim - ptr)) {
			Reserve(size + 128);
		}
		memcpy(ptr, data, size);
		ptr += size;
	}

public:
	virtual  void  SetSize(int64 asize)
	{
		dword size = (dword)asize;
		dword p = (dword)(uintptr_t)(ptr - buffer);
		data.SetCount(size);
		Open(data);
		SetStoring();
		Seek(min(p, size));
	}

protected:
	Vector<byte> data;

public:
	void      Open(Vector<byte> & d)
	{
		if(&data != &d) data = d; //pick
		MemStream::Create((byte*)data, data.GetCount());
	}

	void        Create()
	{
		data.Clear();
		Open(data);
		SetStoring();	
	}

	void        Reserve(int n)
	{
		SetSize((int)GetSize() + n);
	}

	Vector<byte> GetResult()
	{
		Vector<byte> d = data; //pick
		Create();
		return d;
	}
	operator    Vector<byte>()               { return GetResult(); }

	BufferStream()                           { Create(); }
	BufferStream(Vector<byte>& d)            { Open(d); }
};

typedef BufferStream VectorStream;



EDIT: luckily noone has downloaded this one so far..
so i exchange it with an update..GetResult now trims data to only used..



[Updated on: Thu, 12 August 2010 11:05]

Report message to a moderator

Re: NEW: BufferStream [message #28016 is a reply to message #27938] Fri, 13 August 2010 10:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Wed, 11 August 2010 05:25

hi guys,

i was in need of a BufferStream, a stream that can store data inside a Vector<byte>, which i find beeing more convenient than the StringBuffer, which is quite hard to understand Smile (having both a String data and a StringBuffer etc..where data is copied around i think..which is not what i needed). BTW: why was that nessessary, why couldnt it have been only StringBuffer or only String data?



Because sometimes the code uses Stream interface.

E.g. you have Serialize and you want to store the output to String (or read the input).

Re: NEW: BufferStream [message #28020 is a reply to message #28016] Fri, 13 August 2010 11:02 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
didn't quite get it yet, sorry, which code? Stream interface is used always, or not?
Re: NEW: BufferStream [message #28057 is a reply to message #28020] Fri, 13 August 2010 21:48 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Fri, 13 August 2010 05:02

didn't quite get it yet, sorry, which code? Stream interface is used always, or not?


Now I see, you wanted to add Stream interface to String?

Well, that is not a good idea for many reasons...
Re: NEW: BufferStream [message #28090 is a reply to message #28057] Sun, 15 August 2010 08:14 Go to previous message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
why? selfgrowing buffers? forgetting to flush? reallocation?
Previous Topic: Wrong(?) ASSERT in Vector<>::Insert()
Next Topic: Portable Delay() function ?
Goto Forum:
  


Current Time: Mon Apr 29 11:47:48 CEST 2024

Total time taken to generate the page: 0.03379 seconds