Skip to content

Commit

Permalink
Merge pull request #138 from dlifshitz-maca/TIMOB-10162_01
Browse files Browse the repository at this point in the history
TIMOB-10162: BlackBerry: Implement Ti.Stream.pump for Anvil
  • Loading branch information
Larochelle committed Aug 24, 2012
2 parents 8458edf + 7089923 commit 22abe85
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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);
}
};

0 comments on commit 22abe85

Please sign in to comment.