Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIMOB-10162: BlackBerry: Implement Ti.Stream.pump for Anvil #138

Merged
merged 2 commits into from
Aug 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions apidoc/Titanium/Stream/Stream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ methods:
type: Boolean
optional: true
default: false
platforms: [android, iphone, ipad]
platforms: [android, iphone, ipad, blackberry]

---
name: CreateStreamArgs
Expand Down Expand Up @@ -421,7 +421,7 @@ description: |
pump operation, either because of an error or because the end of the
stream has been reached.
since: "1.7"
platforms: [android, iphone, ipad]
platforms: [android, iphone, ipad, blackberry]

properties:
- name: source
Expand Down
3 changes: 3 additions & 0 deletions blackberry/tibb/NativeBufferObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ int NativeBufferObject::getValue(TiObject* /*obj*/)

void NativeBufferObject::clear()
{
//reset data, but keep size the same
int size = internalData_.size();
internalData_.clear();
internalData_.resize(size);
}

const char* NativeBufferObject::toString() const
Expand Down
4 changes: 4 additions & 0 deletions blackberry/tibb/NativeTCPSocketObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ int NativeTCPSocketObject::read(NativeBufferObject* buffer, int offset, int leng
bytesRead = readData.size();
}
}
else
{
eventHandler_->error(tcpClient_->error());
}

return bytesRead;
}
56 changes: 56 additions & 0 deletions blackberry/tibb/titanium/javascript/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,59 @@ L = function(key, hint)
Ti.BufferStream.demoFunc = function() {
Ti.API.debug("demoFunc is being run");
};

Ti.Stream.pump = function(inputStream, handler, maxChunkSize, isAsync)
{
if (isAsync === undefined)
{
isAsync = false;
}
var buffer = Ti.createBuffer({ length: maxChunkSize });
var pumpCallBackArgs = {
buffer: buffer,
bytesProcessed: -1,
errorDescription: '',
errorState: 0,
source: inputStream,
totalBytesProcessed: 0,
};

var readHelper = function() {
buffer.clear();
pumpCallBackArgs.bytesProcessed = -1;
try
{
pumpCallBackArgs.bytesProcessed = inputStream.read(buffer, 0, maxChunkSize);
}
catch(err)
{
pumpCallBackArgs.errorDescription = err;
pumpCallBackArgs.errorState = 1;
}
if (pumpCallBackArgs.bytesProcessed >= 0)
{
pumpCallBackArgs.totalBytesProcessed += pumpCallBackArgs.bytesProcessed;
}
handler(pumpCallBackArgs);
};

if (isAsync)
{
var pumpHelper = function()
{
readHelper();
if (pumpCallBackArgs.bytesProcessed >= 0)
{
setTimeout(pumpHelper, 1);
}
};
setTimeout(pumpHelper, 1);
}
else
{
do
{
readHelper();
} while (pumpCallBackArgs.bytesProcessed >= 0);
}
};