diff options
Diffstat (limited to 'obex/javax/obex/PrivateOutputStream.java')
-rw-r--r-- | obex/javax/obex/PrivateOutputStream.java | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/obex/javax/obex/PrivateOutputStream.java b/obex/javax/obex/PrivateOutputStream.java index 06dd55e..ca420af 100644 --- a/obex/javax/obex/PrivateOutputStream.java +++ b/obex/javax/obex/PrivateOutputStream.java @@ -34,6 +34,7 @@ package javax.obex; import java.io.IOException; import java.io.OutputStream; +import java.io.ByteArrayOutputStream; /** * This object provides an output stream to the Operation objects used in this @@ -44,7 +45,7 @@ public final class PrivateOutputStream extends OutputStream { private BaseStream mParent; - private ObexByteBuffer mBuffer; + private ByteArrayOutputStream mArray; private boolean mOpen; @@ -56,7 +57,7 @@ public final class PrivateOutputStream extends OutputStream { */ public PrivateOutputStream(BaseStream p, int maxSize) { mParent = p; - mBuffer = new ObexByteBuffer(32); + mArray = new ByteArrayOutputStream(); mMaxPacketSize = maxSize; mOpen = true; } @@ -66,7 +67,7 @@ public final class PrivateOutputStream extends OutputStream { * @return the number of bytes written to the output stream */ public int size() { - return mBuffer.getLength(); + return mArray.size(); } /** @@ -81,8 +82,8 @@ public final class PrivateOutputStream extends OutputStream { public synchronized void write(int b) throws IOException { ensureOpen(); mParent.ensureNotDone(); - mBuffer.write((byte)b); - if (mBuffer.getLength() == mMaxPacketSize) { + mArray.write(b); + if (mArray.size() == mMaxPacketSize) { mParent.continueOperation(true, false); } } @@ -107,30 +108,38 @@ public final class PrivateOutputStream extends OutputStream { ensureOpen(); mParent.ensureNotDone(); if (count < mMaxPacketSize) { - mBuffer.write(buffer, offset, count); + mArray.write(buffer, offset, count); } else { while (remainLength >= mMaxPacketSize) { - mBuffer.write(buffer, offset1, mMaxPacketSize); + mArray.write(buffer, offset1, mMaxPacketSize); offset1 += mMaxPacketSize; remainLength = count - offset1; mParent.continueOperation(true, false); } if (remainLength > 0) { - mBuffer.write(buffer, offset1, remainLength); + mArray.write(buffer, offset1, remainLength); } } } /** - * Write some of the bytes that have been written to this stream to - * an ObexByteBuffer. - * - * @param dest the stream to write to - * @param start where to write in the byte array - * @param size the number of bytes to write to the byte array + * Reads the bytes that have been written to this stream. + * @param size the size of the array to return + * @return the byte array that is written */ - public synchronized void writeTo(ObexByteBuffer dest, int size) throws IOException { - mBuffer.read(dest, size); + public synchronized byte[] readBytes(int size) { + if (mArray.size() > 0) { + byte[] temp = mArray.toByteArray(); + mArray.reset(); + byte[] result = new byte[size]; + System.arraycopy(temp, 0, result, 0, size); + if (temp.length != size) { + mArray.write(temp, size, temp.length - size); + } + return result; + } else { + return null; + } } /** |