diff options
-rw-r--r-- | obex/javax/obex/ClientOperation.java | 46 | ||||
-rw-r--r-- | obex/javax/obex/ClientSession.java | 111 | ||||
-rw-r--r-- | obex/javax/obex/HeaderSet.java | 2 | ||||
-rw-r--r-- | obex/javax/obex/ObexByteBuffer.java | 326 | ||||
-rw-r--r-- | obex/javax/obex/ObexHelper.java | 58 | ||||
-rw-r--r-- | obex/javax/obex/ObexSession.java | 2 | ||||
-rw-r--r-- | obex/javax/obex/PrivateInputStream.java | 42 | ||||
-rw-r--r-- | obex/javax/obex/PrivateOutputStream.java | 41 | ||||
-rw-r--r-- | obex/javax/obex/ServerOperation.java | 84 | ||||
-rw-r--r-- | obex/javax/obex/ServerSession.java | 96 |
10 files changed, 259 insertions, 549 deletions
diff --git a/obex/javax/obex/ClientOperation.java b/obex/javax/obex/ClientOperation.java index 9e0700f..05b498c 100644 --- a/obex/javax/obex/ClientOperation.java +++ b/obex/javax/obex/ClientOperation.java @@ -37,6 +37,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; +import java.io.ByteArrayOutputStream; /** * This class implements the <code>Operation</code> interface. It will read and @@ -71,8 +72,6 @@ public final class ClientOperation implements Operation, BaseStream { private boolean mEndOfBodySent; - private ObexByteBuffer mOutBuffer; - /** * Creates new OperationImpl to read and write data to a server * @param maxSize the maximum packet size @@ -101,8 +100,6 @@ public final class ClientOperation implements Operation, BaseStream { mRequestHeader = new HeaderSet(); - mOutBuffer = new ObexByteBuffer(32); - int[] headerList = header.getHeaderList(); if (headerList != null) { @@ -399,7 +396,7 @@ public final class ClientOperation implements Operation, BaseStream { */ private boolean sendRequest(int opCode) throws IOException { boolean returnValue = false; - + ByteArrayOutputStream out = new ByteArrayOutputStream(); int bodyLength = -1; byte[] headerArray = ObexHelper.createHeader(mRequestHeader, true); if (mPrivateOutput != null) { @@ -440,9 +437,9 @@ public final class ClientOperation implements Operation, BaseStream { throw new IOException("OBEX Packet exceeds max packet size"); } - mOutBuffer.reset(); - mOutBuffer.write(headerArray, start, end - start); - if (!mParent.sendRequest(opCode, mOutBuffer, mReplyHeader, mPrivateInput)) { + byte[] sendHeader = new byte[end - start]; + System.arraycopy(headerArray, start, sendHeader, 0, sendHeader.length); + if (!mParent.sendRequest(opCode, sendHeader, mReplyHeader, mPrivateInput)) { return false; } @@ -459,8 +456,7 @@ public final class ClientOperation implements Operation, BaseStream { return false; } } else { - mOutBuffer.reset(); - mOutBuffer.write(headerArray); + out.write(headerArray); } if (bodyLength > 0) { @@ -475,6 +471,8 @@ public final class ClientOperation implements Operation, BaseStream { bodyLength = mMaxPacketSize - headerArray.length - 6; } + byte[] body = mPrivateOutput.readBytes(bodyLength); + /* * Since this is a put request if the final bit is set or * the output stream is closed we need to send the 0x49 @@ -482,40 +480,44 @@ public final class ClientOperation implements Operation, BaseStream { */ if ((mPrivateOutput.isClosed()) && (!returnValue) && (!mEndOfBodySent) && ((opCode & 0x80) != 0)) { - mOutBuffer.write((byte)0x49); + out.write(0x49); mEndOfBodySent = true; } else { - mOutBuffer.write((byte)0x48); + out.write(0x48); } bodyLength += 3; - mOutBuffer.write((byte)(bodyLength >> 8)); - mOutBuffer.write((byte)bodyLength); - mPrivateOutput.writeTo(mOutBuffer, bodyLength - 3); + out.write((byte)(bodyLength >> 8)); + out.write((byte)bodyLength); + + if (body != null) { + out.write(body); + } } if (mPrivateOutputOpen && bodyLength <= 0 && !mEndOfBodySent) { // only 0x82 or 0x83 can send 0x49 if ((opCode & 0x80) == 0) { - mOutBuffer.write((byte)0x48); + out.write(0x48); } else { - mOutBuffer.write((byte)0x49); + out.write(0x49); mEndOfBodySent = true; + } bodyLength = 3; - mOutBuffer.write((byte)(bodyLength >> 8)); - mOutBuffer.write((byte)bodyLength); + out.write((byte)(bodyLength >> 8)); + out.write((byte)bodyLength); } - if (mOutBuffer.getLength() == 0) { + if (out.size() == 0) { if (!mParent.sendRequest(opCode, null, mReplyHeader, mPrivateInput)) { return false; } return returnValue; } - if ((mOutBuffer.getLength() > 0) - && (!mParent.sendRequest(opCode, mOutBuffer, mReplyHeader, mPrivateInput))) { + if ((out.size() > 0) + && (!mParent.sendRequest(opCode, out.toByteArray(), mReplyHeader, mPrivateInput))) { return false; } diff --git a/obex/javax/obex/ClientSession.java b/obex/javax/obex/ClientSession.java index 597c17c..0935383 100644 --- a/obex/javax/obex/ClientSession.java +++ b/obex/javax/obex/ClientSession.java @@ -32,6 +32,7 @@ package javax.obex; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -61,23 +62,11 @@ public final class ClientSession extends ObexSession { private final OutputStream mOutput; - private ObexByteBuffer mOutBuffer; - - private ObexByteBuffer mData; - - private ObexByteBuffer mBodyBuffer; - - private ObexByteBuffer mHeaderBuffer; - public ClientSession(final ObexTransport trans) throws IOException { mInput = trans.openInputStream(); mOutput = trans.openOutputStream(); mOpen = true; mRequestActive = false; - mOutBuffer = new ObexByteBuffer(32); - mData = new ObexByteBuffer(32); - mBodyBuffer = new ObexByteBuffer(32); - mHeaderBuffer = new ObexByteBuffer(32); } public HeaderSet connect(final HeaderSet header) throws IOException { @@ -108,20 +97,19 @@ public final class ClientSession extends ObexSession { * Byte 5&6: Max OBEX Packet Length (Defined in MAX_PACKET_SIZE) * Byte 7 to n: headers */ - ObexByteBuffer requestPacket = new ObexByteBuffer(totalLength); - + byte[] requestPacket = new byte[totalLength]; // We just need to start at byte 3 since the sendRequest() method will // handle the length and 0x80. - requestPacket.write((byte)0x10); - requestPacket.write((byte)0x00); - requestPacket.write((byte)(ObexHelper.MAX_PACKET_SIZE_INT >> 8)); - requestPacket.write((byte)(ObexHelper.MAX_PACKET_SIZE_INT & 0xFF)); + requestPacket[0] = (byte)0x10; + requestPacket[1] = (byte)0x00; + requestPacket[2] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT >> 8); + requestPacket[3] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT & 0xFF); if (head != null) { - requestPacket.write(head); + System.arraycopy(head, 0, requestPacket, 4, head.length); } // check with local max packet size - if ((totalLength + 3) > ObexHelper.MAX_PACKET_SIZE_INT) { + if ((requestPacket.length + 3) > ObexHelper.MAX_PACKET_SIZE_INT) { throw new IOException("Packet size exceeds max packet size"); } @@ -226,14 +214,8 @@ public final class ClientSession extends ObexSession { } } - ObexByteBuffer headBuffer = null; - if (head != null) { - headBuffer = new ObexByteBuffer(head.length); - headBuffer.write(head); - } - HeaderSet returnHeaderSet = new HeaderSet(); - sendRequest(ObexHelper.OBEX_OPCODE_DISCONNECT, headBuffer, returnHeaderSet, null); + sendRequest(ObexHelper.OBEX_OPCODE_DISCONNECT, head, returnHeaderSet, null); /* * An OBEX DISCONNECT reply from the server: @@ -358,11 +340,11 @@ public final class ClientSession extends ObexSession { * Byte 5: constants * Byte 6 & up: headers */ - ObexByteBuffer packet = new ObexByteBuffer(totalLength); - packet.write((byte)flags); - packet.write((byte)0x00); + byte[] packet = new byte[totalLength]; + packet[0] = (byte)flags; + packet[1] = (byte)0x00; if (headset != null) { - packet.write(head); + System.arraycopy(head, 0, packet, 2, head.length); } HeaderSet returnHeaderSet = new HeaderSet(); @@ -423,30 +405,31 @@ public final class ClientSession extends ObexSession { * <code>false</code> if an authentication response failed to pass * @throws IOException if an IO error occurs */ - public boolean sendRequest(int opCode, ObexByteBuffer head, HeaderSet header, + public boolean sendRequest(int opCode, byte[] head, HeaderSet header, PrivateInputStream privateInput) throws IOException { //check header length with local max size if (head != null) { - if ((head.getLength() + 3) > ObexHelper.MAX_PACKET_SIZE_INT) { + if ((head.length + 3) > ObexHelper.MAX_PACKET_SIZE_INT) { throw new IOException("header too large "); } } - mOutBuffer.reset(); - mOutBuffer.write((byte)opCode); + int bytesReceived; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write((byte)opCode); // Determine if there are any headers to send if (head == null) { - mOutBuffer.write((byte)0x00); - mOutBuffer.write((byte)0x03); + out.write(0x00); + out.write(0x03); } else { - mOutBuffer.write((byte)((head.getLength() + 3) >> 8)); - mOutBuffer.write((byte)(head.getLength() + 3)); - mOutBuffer.write(head, 0); + out.write((byte)((head.length + 3) >> 8)); + out.write((byte)(head.length + 3)); + out.write(head); } // Write the request to the output stream and flush the stream - mOutBuffer.peek(mOutput, mOutBuffer.getLength()); + mOutput.write(out.toByteArray()); mOutput.flush(); header.responseCode = mInput.read(); @@ -457,7 +440,7 @@ public final class ClientSession extends ObexSession { throw new IOException("Packet received exceeds packet size limit"); } if (length > ObexHelper.BASE_PACKET_LENGTH) { - mData.reset(); + byte[] data = null; if (opCode == ObexHelper.OBEX_OPCODE_CONNECT) { @SuppressWarnings("unused") int version = mInput.read(); @@ -471,21 +454,31 @@ public final class ClientSession extends ObexSession { } if (length > 7) { - mData.write(mInput, length - 7); + data = new byte[length - 7]; + + bytesReceived = mInput.read(data); + while (bytesReceived != (length - 7)) { + bytesReceived += mInput.read(data, bytesReceived, data.length + - bytesReceived); + } } else { return true; } } else { - mData.write(mInput, length - 3); + data = new byte[length - 3]; + bytesReceived = mInput.read(data); + + while (bytesReceived != (length - 3)) { + bytesReceived += mInput.read(data, bytesReceived, data.length - bytesReceived); + } if (opCode == ObexHelper.OBEX_OPCODE_ABORT) { return true; } } - ObexHelper.updateHeaderSet(header, mData, mBodyBuffer, mHeaderBuffer); - - if ((privateInput != null) && (mBodyBuffer.getLength() > 0)) { - privateInput.writeBytes(mBodyBuffer, 1); + byte[] body = ObexHelper.updateHeaderSet(header, data); + if ((privateInput != null) && (body != null)) { + privateInput.writeBytes(body, 1); } if (header.mConnectionID != null) { @@ -504,23 +497,17 @@ public final class ClientSession extends ObexSession { && (header.mAuthChall != null)) { if (handleAuthChall(header)) { - - // This can't be a member variable and mOutBuffer can't be used here - // since this is a recursive call. - // That's OK since authentication should not happen very often. - ObexByteBuffer sendHeadersBuffer = new ObexByteBuffer( - (mOutBuffer.getLength() - 3) + header.mAuthResp.length + 3); - sendHeadersBuffer.write(mOutBuffer, 3); - - sendHeadersBuffer.write((byte)HeaderSet.AUTH_RESPONSE); - sendHeadersBuffer.write((byte)((header.mAuthResp.length + 3) >> 8)); - sendHeadersBuffer.write((byte)(header.mAuthResp.length + 3)); - sendHeadersBuffer.write(header.mAuthResp); - + out.write((byte)HeaderSet.AUTH_RESPONSE); + out.write((byte)((header.mAuthResp.length + 3) >> 8)); + out.write((byte)(header.mAuthResp.length + 3)); + out.write(header.mAuthResp); header.mAuthChall = null; header.mAuthResp = null; - return sendRequest(opCode, sendHeadersBuffer, header, privateInput); + byte[] sendHeaders = new byte[out.size() - 3]; + System.arraycopy(out.toByteArray(), 3, sendHeaders, 0, sendHeaders.length); + + return sendRequest(opCode, sendHeaders, header, privateInput); } } } diff --git a/obex/javax/obex/HeaderSet.java b/obex/javax/obex/HeaderSet.java index 8cf15f5..b89b707 100644 --- a/obex/javax/obex/HeaderSet.java +++ b/obex/javax/obex/HeaderSet.java @@ -126,7 +126,7 @@ public final class HeaderSet { /** * Represents the OBEX End of BODY header. * <P> - * The value of <code>END_OF_BODY</code> is 0x49 (73). + * The value of <code>BODY</code> is 0x49 (73). */ public static final int END_OF_BODY = 0x49; diff --git a/obex/javax/obex/ObexByteBuffer.java b/obex/javax/obex/ObexByteBuffer.java deleted file mode 100644 index 1bf68b6..0000000 --- a/obex/javax/obex/ObexByteBuffer.java +++ /dev/null @@ -1,326 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package javax.obex; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -class ObexByteBuffer { - private static final int REALLOC_EXTRA_SPACE = 24; - - private byte[] mBuffer; - - private int mIndex; - - private int mLength; - - public ObexByteBuffer(int initialSize) { - mBuffer = new byte[initialSize]; - mIndex = 0; - mLength = 0; - } - - /** - * Mark bytes at beginning or valid data as invalid. - * @param numBytes Number of bytes to consume. - */ - private void consume(int numBytes) { - mLength -= numBytes; - if (mLength > 0) { - mIndex += numBytes; - } else { - mIndex = 0; - } - } - - /** - * Make room in for new data (if needed). - * @param numBytes Number of bytes to make room for. - */ - private void aquire(int numBytes) { - int remainingSpace = mBuffer.length - (mIndex + mLength); - - // Do we need to grow or shuffle? - if (remainingSpace < numBytes) { - int availableSpace = mBuffer.length - mLength; - if (availableSpace < numBytes) { - // Need to grow. Add some extra space to avoid small growth. - byte[] newbuf = new byte[mLength + numBytes + REALLOC_EXTRA_SPACE]; - System.arraycopy(mBuffer, mIndex, newbuf, 0, mLength); - mBuffer = newbuf; - } else { - // Need to shuffle - System.arraycopy(mBuffer, mIndex, mBuffer, 0, mLength); - } - mIndex = 0; - } - } - - /** - * Get the internal byte array. Use with care. - * @return the internal byte array - */ - public byte[] getBytes() { - return mBuffer; - } - - /** - * Get number of written but not consumed bytes. - * @return number of bytes - */ - public int getLength() { - return mLength; - } - - /** - * Discard all unconsumed bytes. - */ - public void reset() { - mIndex = 0; - mLength = 0; - } - - /** - * Read and consume one byte. - * @return Next unconsumed byte. - */ - public byte read() { - if (mLength == 0) { - throw new ArrayIndexOutOfBoundsException(); - } - mLength--; - return mBuffer[mIndex++]; - } - - /** - * Read and consume bytes, and write them into a byte array. - * Will read (dest.length - destOffset) bytes. - * @param dest Array to copy data into. - * @param destOffset Where to start writing in dest. - * @return number of read bytes. - */ - public int read(byte[] dest, int destOffset) { - return read(dest, destOffset, mLength); - } - - /** - * Read and consume bytes, and write them into a byte array. - * Will read (length - destOffset) bytes. - * @param dest Array to copy data into. - * @param destOffset Where to start writing in dest. - * @param length Number of bytes to read. - * @return number of read bytes. - */ - public int read(byte[] dest, int destOffset, int length) { - peek(0, dest, destOffset, length); - consume(length); - return length; - } - - /** - * Read and consume bytes, and write them into another ObexByteBuffer. - * @param dest ObexByteBuffer to copy data into. - * @param length Number of bytes to read. - * @return number of read bytes. - */ - public int read(ObexByteBuffer dest, int length) { - if (length > mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - dest.write(mBuffer, mIndex, length); - consume(length); - - return length; - } - - /** - * Read and consume all unconsumed bytes, and write them into an OutputStream. - * @param dest OutputStream to copy data into. - * @return number of read bytes. - */ - public int read(OutputStream stream) throws IOException { - return read(stream, mLength); - } - - /** - * Read and consume bytes, and write them into an OutputStream. - * @param dest OutputStream to copy data into. - * @param length Number of bytes to read. - * @return number of read bytes. - */ - public int read(OutputStream destStream, int length) throws IOException { - peek(destStream, length); - consume(length); - return length; - } - - /** - * Read (but don't consume) one byte. - * @param offset Offset into unconsumed bytes. - * @return Requested unconsumed byte. - */ - public byte peek(int offset) { - if (offset > mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - return mBuffer[mIndex + offset]; - } - - /** - * Read (but don't consume) bytes and write them into a byte array. - * Will read dest.length bytes. - * @param offset Offset into unconsumed bytes. - * @param dest Array to copy data into. - */ - public void peek(int offset, byte[] dest) { - peek(offset, dest, 0, dest.length); - } - - /** - * Read (but don't consume) bytes and write them into a byte array. - * Will read (length - destOffset) bytes. - * @param offset Offset into unconsumed bytes. - * @param dest Array to copy data into. - * @param destOffset Where to start writing in dest. - * @param length Number of bytes to read. - */ - public void peek(int offset, byte[] dest, int destOffset, int length) { - if (offset > mLength || (offset + length) > mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - System.arraycopy(mBuffer, mIndex + offset, dest, destOffset, length); - } - - /** - * Read (but don't consume) bytes, and write them into an OutputStream. - * @param dest OutputStream to copy data into. - * @param length Number of bytes to read. - */ - public void peek(OutputStream stream, int length) throws IOException { - if (length > mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - stream.write(mBuffer, mIndex, length); - } - - /** - * Write a new byte. - * @param src Byte to write. - */ - public void write(byte src) { - aquire(1); - mBuffer[mIndex + mLength] = src; - mLength++; - } - - /** - * Read bytes from a byte array and add to unconsumed bytes. - * Will read/write src.length bytes. - * @param src Array to read from. - */ - public void write(byte[] src) { - write(src, 0, src.length); - } - - /** - * Read bytes from a byte array and add to unconsumed bytes. - * Will read/write (src.length - srcOffset) bytes. - * @param src Array to read from. - * @param srcOffset Offset into source array. - */ - public void write(byte[] src, int srcOffset) { - write(src, srcOffset, src.length - srcOffset); - } - - /** - * Read bytes from a byte array and add to unconsumed bytes. - * Will read/write (srcLength - srcOffset) bytes. - * @param src Array to read from. - * @param srcOffset Offset into source array. - * @param srcLength Number of bytes to read/write. - */ - public void write(byte[] src, int srcOffset, int srcLength) { - // Make sure we have space. - aquire(srcLength); - - // Add the new data at the end - System.arraycopy(src, srcOffset, mBuffer, mIndex + mLength, srcLength); - mLength += srcLength; - } - - /** - * Read bytes from another ObexByteBuffer and add to unconsumed bytes. - * Will read/write src.getLength() bytes. The bytes in src will not be consumed. - * @param src ObexByteBuffer to read from. - * @param srcOffset Offset into source array. - */ - public void write(ObexByteBuffer src) { - write(src.mBuffer, 0, src.getLength()); - } - - /** - * Read bytes from another ObexByteBuffer and add to unconsumed bytes. - * Will read/write (src.getLength() - srcOffset) bytes. The bytes in src will not - * be consumed. - * @param src ObexByteBuffer to read from. - * @param srcOffset Offset into source array. - */ - public void write(ObexByteBuffer src, int srcOffset) { - if (srcOffset > src.mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - write(src.mBuffer, src.mIndex + srcOffset, src.mLength - src.mIndex - srcOffset); - } - - /** - * Read bytes from another ObexByteBuffer and add to unconsumed bytes. - * Will read/write (srcLength - srcOffset) bytes. The bytes in src will not be - * consumed. - * @param src ObexByteBuffer to read from. - * @param srcOffset Offset into source array. - * @param srcLength Number of bytes to read/write. - */ - public void write(ObexByteBuffer src, int srcOffset, int srcLength) { - if (srcOffset > src.mLength || (srcOffset + srcLength) > src.mLength) { - throw new ArrayIndexOutOfBoundsException(); - } - write(src.mBuffer, src.mIndex + srcOffset, srcLength); - } - - /** - * Read bytes from an InputStream and add to unconsumed bytes. - * @param src InputStream to read from - * @param srcLength Number of bytes to read - * @throws IOException - */ - public void write(InputStream src, int srcLength) throws IOException { - // First make sure we have space. - aquire(srcLength); - - // Read data until the requested number of bytes have been read. - int numBytes = 0; - do { - int readBytes = src.read(mBuffer, mIndex + mLength + numBytes, srcLength - numBytes); - if (readBytes == -1) { - throw new IOException(); - } - numBytes += readBytes; - } while (numBytes != srcLength); - mLength += numBytes; - } -} diff --git a/obex/javax/obex/ObexHelper.java b/obex/javax/obex/ObexHelper.java index afb8101..1b66662 100644 --- a/obex/javax/obex/ObexHelper.java +++ b/obex/javax/obex/ObexHelper.java @@ -151,22 +151,21 @@ public final class ObexHelper { * exception to be thrown. When it is thrown, it is ignored. * @param header the HeaderSet to update * @param headerArray the byte array containing headers - * @param bodyBuffer Buffer to return result in. Will be reset(). Can be null. - * Will contain the result of the last start body or end body header provided; - * the first byte in the result will specify if a body or end of body is received - * @param headerBuffer Buffer to use to avoid allocations. Will be reset(). + * @return the result of the last start body or end body header provided; + * the first byte in the result will specify if a body or end of + * body is received * @throws IOException if an invalid header was found */ - static void updateHeaderSet(HeaderSet header, ObexByteBuffer headerArray, - ObexByteBuffer bodyBuffer, ObexByteBuffer headerBuffer) throws IOException { + public static byte[] updateHeaderSet(HeaderSet header, byte[] headerArray) throws IOException { int index = 0; int length = 0; int headerID; byte[] value = null; + byte[] body = null; HeaderSet headerImpl = header; try { - while (index < headerArray.getLength()) { - headerID = 0xFF & headerArray.peek(index); + while (index < headerArray.length) { + headerID = 0xFF & headerArray[index]; switch (headerID & (0xC0)) { /* @@ -182,15 +181,14 @@ public final class ObexHelper { case 0x40: boolean trimTail = true; index++; - length = 0xFF & headerArray.peek(index); + length = 0xFF & headerArray[index]; length = length << 8; index++; - length += 0xFF & headerArray.peek(index); + length += 0xFF & headerArray[index]; length -= 3; index++; - headerBuffer.reset(); - headerBuffer.write(headerArray, index, length); - value = headerBuffer.getBytes(); + value = new byte[length]; + System.arraycopy(headerArray, index, value, 0, length); if (length == 0 || (length > 0 && (value[length - 1] != 0))) { trimTail = false; } @@ -200,10 +198,10 @@ public final class ObexHelper { // Remove trailing null if (trimTail == false) { headerImpl.setHeader(headerID, new String(value, 0, - length, "ISO8859_1")); + value.length, "ISO8859_1")); } else { headerImpl.setHeader(headerID, new String(value, 0, - length - 1, "ISO8859_1")); + value.length - 1, "ISO8859_1")); } } catch (UnsupportedEncodingException e) { throw e; @@ -212,27 +210,27 @@ public final class ObexHelper { case HeaderSet.AUTH_CHALLENGE: headerImpl.mAuthChall = new byte[length]; - headerArray.peek(index, headerImpl.mAuthChall); + System.arraycopy(headerArray, index, headerImpl.mAuthChall, 0, + length); break; case HeaderSet.AUTH_RESPONSE: headerImpl.mAuthResp = new byte[length]; - headerArray.peek(index, headerImpl.mAuthResp); + System.arraycopy(headerArray, index, headerImpl.mAuthResp, 0, + length); break; case HeaderSet.BODY: /* Fall Through */ case HeaderSet.END_OF_BODY: - if (bodyBuffer != null) { - bodyBuffer.reset(); - bodyBuffer.write((byte)headerID); - bodyBuffer.write(headerArray, index, length); - } + body = new byte[length + 1]; + body[0] = (byte)headerID; + System.arraycopy(headerArray, index, body, 1, length); break; case HeaderSet.TIME_ISO_8601: try { - String dateString = new String(value, 0, length, "ISO8859_1"); + String dateString = new String(value, "ISO8859_1"); Calendar temp = Calendar.getInstance(); if ((dateString.length() == 16) && (dateString.charAt(15) == 'Z')) { @@ -259,7 +257,7 @@ public final class ObexHelper { default: if ((headerID & 0xC0) == 0x00) { headerImpl.setHeader(headerID, ObexHelper.convertToUnicode( - value, length, true)); + value, true)); } else { headerImpl.setHeader(headerID, value); } @@ -275,7 +273,7 @@ public final class ObexHelper { case 0x80: index++; try { - headerImpl.setHeader(headerID, Byte.valueOf(headerArray.peek(index))); + headerImpl.setHeader(headerID, Byte.valueOf(headerArray[index])); } catch (Exception e) { // Not a valid header so ignore } @@ -290,7 +288,7 @@ public final class ObexHelper { case 0xC0: index++; value = new byte[4]; - headerArray.peek(index, value); + System.arraycopy(headerArray, index, value, 0, 4); try { if (headerID != HeaderSet.TIME_4_BYTE) { // Determine if it is a connection ID. These @@ -319,6 +317,8 @@ public final class ObexHelper { } catch (IOException e) { throw new IOException("Header was not formatted properly"); } + + return body; } /** @@ -874,11 +874,11 @@ public final class ObexHelper { * @return a Unicode string * @throws IllegalArgumentException if the byte array has an odd length */ - public static String convertToUnicode(byte[] b, int len, boolean includesNull) { - if (b == null || len == 0) { + public static String convertToUnicode(byte[] b, boolean includesNull) { + if (b == null || b.length == 0) { return null; } - int arrayLength = len; + int arrayLength = b.length; if (!((arrayLength % 2) == 0)) { throw new IllegalArgumentException("Byte array not of a valid form"); } diff --git a/obex/javax/obex/ObexSession.java b/obex/javax/obex/ObexSession.java index 6c4ba43..a7daeb5 100644 --- a/obex/javax/obex/ObexSession.java +++ b/obex/javax/obex/ObexSession.java @@ -98,7 +98,7 @@ public class ObexSession { case ObexHelper.OBEX_AUTH_REALM_CHARSET_UNICODE: // UNICODE Encoding - realm = ObexHelper.convertToUnicode(realmString, realmString.length, false); + realm = ObexHelper.convertToUnicode(realmString, false); break; default: diff --git a/obex/javax/obex/PrivateInputStream.java b/obex/javax/obex/PrivateInputStream.java index 2bcf541..5daee72 100644 --- a/obex/javax/obex/PrivateInputStream.java +++ b/obex/javax/obex/PrivateInputStream.java @@ -44,7 +44,9 @@ public final class PrivateInputStream extends InputStream { private BaseStream mParent; - private ObexByteBuffer mBuffer; + private byte[] mData; + + private int mIndex; private boolean mOpen; @@ -54,7 +56,8 @@ public final class PrivateInputStream extends InputStream { */ public PrivateInputStream(BaseStream p) { mParent = p; - mBuffer = new ObexByteBuffer(0); + mData = new byte[0]; + mIndex = 0; mOpen = true; } @@ -70,7 +73,7 @@ public final class PrivateInputStream extends InputStream { @Override public synchronized int available() throws IOException { ensureOpen(); - return mBuffer.getLength(); + return mData.length - mIndex; } /** @@ -86,12 +89,12 @@ public final class PrivateInputStream extends InputStream { @Override public synchronized int read() throws IOException { ensureOpen(); - while (mBuffer.getLength() == 0) { + while (mData.length == mIndex) { if (!mParent.continueOperation(true, true)) { return -1; } } - return (mBuffer.read() & 0xFF); + return (mData[mIndex++] & 0xFF); } @Override @@ -110,23 +113,26 @@ public final class PrivateInputStream extends InputStream { } ensureOpen(); + int currentDataLength = mData.length - mIndex; int remainReadLength = length; int offset1 = offset; int result = 0; - while (mBuffer.getLength() <= remainReadLength) { - int readBytes = mBuffer.read(b, offset1); - - offset1 += readBytes; - result += readBytes; - remainReadLength -= readBytes; + while (currentDataLength <= remainReadLength) { + System.arraycopy(mData, mIndex, b, offset1, currentDataLength); + mIndex += currentDataLength; + offset1 += currentDataLength; + result += currentDataLength; + remainReadLength -= currentDataLength; if (!mParent.continueOperation(true, true)) { return result == 0 ? -1 : result; } + currentDataLength = mData.length - mIndex; } if (remainReadLength > 0) { - mBuffer.read(b, offset1, remainReadLength); + System.arraycopy(mData, mIndex, b, offset1, remainReadLength); + mIndex += remainReadLength; result += remainReadLength; } return result; @@ -138,8 +144,16 @@ public final class PrivateInputStream extends InputStream { * @param body the data to add to the stream * @param start the start of the body to array to copy */ - public synchronized void writeBytes(ObexByteBuffer body, int start) { - mBuffer.write(body, start); + public synchronized void writeBytes(byte[] body, int start) { + + int length = (body.length - start) + (mData.length - mIndex); + byte[] temp = new byte[length]; + + System.arraycopy(mData, mIndex, temp, 0, mData.length - mIndex); + System.arraycopy(body, start, temp, mData.length - mIndex, body.length - start); + + mData = temp; + mIndex = 0; notifyAll(); } 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; + } } /** diff --git a/obex/javax/obex/ServerOperation.java b/obex/javax/obex/ServerOperation.java index dc1f3c7..07a3a53 100644 --- a/obex/javax/obex/ServerOperation.java +++ b/obex/javax/obex/ServerOperation.java @@ -37,6 +37,7 @@ import java.io.InputStream; import java.io.DataInputStream; import java.io.OutputStream; import java.io.DataOutputStream; +import java.io.ByteArrayOutputStream; /** * This class implements the Operation interface for server side connections. @@ -87,12 +88,6 @@ public final class ServerOperation implements Operation, BaseStream { private boolean mHasBody; - private ObexByteBuffer mData; - - private ObexByteBuffer mBodyBuffer; - - private ObexByteBuffer mHeaderBuffer; - /** * Creates new ServerOperation * @param p the parent that created this object @@ -119,10 +114,7 @@ public final class ServerOperation implements Operation, BaseStream { mRequestFinished = false; mPrivateOutputOpen = false; mHasBody = false; - - mData = new ObexByteBuffer(32); - mBodyBuffer = new ObexByteBuffer(32); - mHeaderBuffer = new ObexByteBuffer(32); + int bytesReceived; /* * Determine if this is a PUT request @@ -173,12 +165,16 @@ public final class ServerOperation implements Operation, BaseStream { * Determine if any headers were sent in the initial request */ if (length > 3) { - mData.reset(); - mData.write(in, length - 3); + byte[] data = new byte[length - 3]; + bytesReceived = in.read(data); + + while (bytesReceived != data.length) { + bytesReceived += in.read(data, bytesReceived, data.length - bytesReceived); + } - ObexHelper.updateHeaderSet(requestHeader, mData, mBodyBuffer, mHeaderBuffer); + byte[] body = ObexHelper.updateHeaderSet(requestHeader, data); - if (mBodyBuffer.getLength() > 0) { + if (body != null) { mHasBody = true; } @@ -209,8 +205,8 @@ public final class ServerOperation implements Operation, BaseStream { } - if (mBodyBuffer.getLength() > 0) { - mPrivateInput.writeBytes(mBodyBuffer, 1); + if (body != null) { + mPrivateInput.writeBytes(body, 1); } else { while ((!mGetOperation) && (!finalBitSet)) { sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); @@ -285,7 +281,8 @@ public final class ServerOperation implements Operation, BaseStream { * @throws IOException if an IO error occurs */ public synchronized boolean sendReply(int type) throws IOException { - mBodyBuffer.reset(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int bytesReceived; long id = mListener.getConnectionId(); if (id == -1) { @@ -325,10 +322,10 @@ public final class ServerOperation implements Operation, BaseStream { mParent.sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null); throw new IOException("OBEX Packet exceeds max packet size"); } - mHeaderBuffer.reset(); - mHeaderBuffer.write(headerArray, start, end - start); + byte[] sendHeader = new byte[end - start]; + System.arraycopy(headerArray, start, sendHeader, 0, sendHeader.length); - mParent.sendResponse(type, mHeaderBuffer); + mParent.sendResponse(type, sendHeader); start = end; } @@ -339,7 +336,7 @@ public final class ServerOperation implements Operation, BaseStream { } } else { - mBodyBuffer.write(headerArray); + out.write(headerArray); } // For Get operation: if response code is OBEX_HTTP_OK, then this is the @@ -359,34 +356,36 @@ public final class ServerOperation implements Operation, BaseStream { bodyLength = mMaxPacketLength - headerArray.length - 6; } + byte[] body = mPrivateOutput.readBytes(bodyLength); + /* * Since this is a put request if the final bit is set or * the output stream is closed we need to send the 0x49 * (End of Body) otherwise, we need to send 0x48 (Body) */ if ((finalBitSet) || (mPrivateOutput.isClosed())) { - mBodyBuffer.write((byte)0x49); + out.write(0x49); } else { - mBodyBuffer.write((byte)0x48); + out.write(0x48); } bodyLength += 3; - mBodyBuffer.write((byte)(bodyLength >> 8)); - mBodyBuffer.write((byte)bodyLength); - mBodyBuffer.read(mPrivateOutput, bodyLength); + out.write((byte)(bodyLength >> 8)); + out.write((byte)bodyLength); + out.write(body); } } if ((finalBitSet) && (type == ResponseCodes.OBEX_HTTP_OK) && (orginalBodyLength <= 0)) { - mBodyBuffer.write((byte)0x49); + out.write(0x49); orginalBodyLength = 3; - mBodyBuffer.write((byte)(orginalBodyLength >> 8)); - mBodyBuffer.write((byte)orginalBodyLength); + out.write((byte)(orginalBodyLength >> 8)); + out.write((byte)orginalBodyLength); } mResponseSize = 3; - mParent.sendResponse(type, mBodyBuffer); + mParent.sendResponse(type, out.toByteArray()); if (type == ResponseCodes.OBEX_HTTP_CONTINUE) { int headerID = mInput.read(); @@ -398,8 +397,12 @@ public final class ServerOperation implements Operation, BaseStream { && (headerID != ObexHelper.OBEX_OPCODE_GET_FINAL)) { if (length > 3) { - mData.reset(); - mData.write(mInput, length); + byte[] temp = new byte[length]; + bytesReceived = mInput.read(temp); + + while (bytesReceived != length) { + bytesReceived += mInput.read(temp, bytesReceived, length - bytesReceived); + } } /* @@ -437,14 +440,17 @@ public final class ServerOperation implements Operation, BaseStream { * Determine if any headers were sent in the initial request */ if (length > 3) { - mData.reset(); - mData.write(mInput, length - 3); + byte[] data = new byte[length - 3]; + bytesReceived = mInput.read(data); - ObexHelper.updateHeaderSet(requestHeader, mData, mBodyBuffer, mHeaderBuffer); - if (mBodyBuffer.getLength() > 0) { + while (bytesReceived != data.length) { + bytesReceived += mInput.read(data, bytesReceived, data.length + - bytesReceived); + } + byte[] body = ObexHelper.updateHeaderSet(requestHeader, data); + if (body != null) { mHasBody = true; } - if (mListener.getConnectionId() != -1 && requestHeader.mConnectionID != null) { mListener.setConnectionId(ObexHelper .convertToLong(requestHeader.mConnectionID)); @@ -473,8 +479,8 @@ public final class ServerOperation implements Operation, BaseStream { requestHeader.mAuthChall = null; } - if (mBodyBuffer.getLength() > 0) { - mPrivateInput.writeBytes(mBodyBuffer, 1); + if (body != null) { + mPrivateInput.writeBytes(body, 1); } } } diff --git a/obex/javax/obex/ServerSession.java b/obex/javax/obex/ServerSession.java index 694c8a9..a4b9759 100644 --- a/obex/javax/obex/ServerSession.java +++ b/obex/javax/obex/ServerSession.java @@ -60,10 +60,6 @@ public final class ServerSession extends ObexSession implements Runnable { private boolean mClosed; - private ObexByteBuffer mData; - - private ObexByteBuffer mHeaderBuffer; - /** * Creates new ServerSession. * @param trans the connection to the client @@ -84,9 +80,6 @@ public final class ServerSession extends ObexSession implements Runnable { mClosed = false; mProcessThread = new Thread(this); mProcessThread.start(); - - mData = new ObexByteBuffer(32); - mHeaderBuffer = new ObexByteBuffer(32); } /** @@ -260,22 +253,24 @@ public final class ServerSession extends ObexSession implements Runnable { * @param header the headers to include in the response * @throws IOException if an IO error occurs */ - public void sendResponse(int code, ObexByteBuffer header) throws IOException { + public void sendResponse(int code, byte[] header) throws IOException { int totalLength = 3; - mData.reset(); + byte[] data = null; if (header != null) { - totalLength += header.getLength(); - mData.write((byte)code); - mData.write((byte)(totalLength >> 8)); - mData.write((byte)totalLength); - mData.write(header); + totalLength += header.length; + data = new byte[totalLength]; + data[0] = (byte)code; + data[1] = (byte)(totalLength >> 8); + data[2] = (byte)totalLength; + System.arraycopy(header, 0, data, 3, header.length); } else { - mData.write((byte)code); - mData.write((byte)0x00); - mData.write((byte)totalLength); + data = new byte[totalLength]; + data[0] = (byte)code; + data[1] = (byte)0x00; + data[2] = (byte)totalLength; } - mData.read(mOutput); + mOutput.write(data); mOutput.flush(); } @@ -296,6 +291,7 @@ public final class ServerSession extends ObexSession implements Runnable { int totalLength = 3; byte[] head = null; int code = -1; + int bytesReceived; HeaderSet request = new HeaderSet(); HeaderSet reply = new HeaderSet(); @@ -309,10 +305,15 @@ public final class ServerSession extends ObexSession implements Runnable { totalLength = 3; } else { if (length > 5) { - mData.reset(); - mData.write(mInput, length - 5); + byte[] headers = new byte[length - 5]; + bytesReceived = mInput.read(headers); - ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer); + while (bytesReceived != headers.length) { + bytesReceived += mInput.read(headers, bytesReceived, headers.length + - bytesReceived); + } + + ObexHelper.updateHeaderSet(request, headers); if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID)); @@ -385,18 +386,18 @@ public final class ServerSession extends ObexSession implements Runnable { } // Compute Length of OBEX SETPATH packet - mData.reset(); - mData.write((byte)code); - mData.write((byte)(totalLength >> 8)); - mData.write((byte)totalLength); + byte[] replyData = new byte[totalLength]; + replyData[0] = (byte)code; + replyData[1] = (byte)(totalLength >> 8); + replyData[2] = (byte)totalLength; if (head != null) { - mData.write(head); + System.arraycopy(head, 0, replyData, 3, head.length); } /* * Write the OBEX SETPATH packet to the server. Byte 0: response code * Byte 1&2: Connect Packet Length Byte 3 to n: headers */ - mData.read(mOutput); + mOutput.write(replyData); mOutput.flush(); } @@ -413,6 +414,7 @@ public final class ServerSession extends ObexSession implements Runnable { int code = ResponseCodes.OBEX_HTTP_OK; int totalLength = 3; byte[] head = null; + int bytesReceived; HeaderSet request = new HeaderSet(); HeaderSet reply = new HeaderSet(); @@ -424,10 +426,15 @@ public final class ServerSession extends ObexSession implements Runnable { totalLength = 3; } else { if (length > 3) { - mData.reset(); - mData.write(mInput, length - 3); + byte[] headers = new byte[length - 3]; + bytesReceived = mInput.read(headers); + + while (bytesReceived != headers.length) { + bytesReceived += mInput.read(headers, bytesReceived, headers.length + - bytesReceived); + } - ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer); + ObexHelper.updateHeaderSet(request, headers); } if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { @@ -478,18 +485,23 @@ public final class ServerSession extends ObexSession implements Runnable { } // Compute Length of OBEX CONNECT packet - mData.reset(); - mData.write((byte)code); - mData.write((byte)(totalLength >> 8)); - mData.write((byte)totalLength); + byte[] replyData; + if (head != null) { + replyData = new byte[3 + head.length]; + } else { + replyData = new byte[3]; + } + replyData[0] = (byte)code; + replyData[1] = (byte)(totalLength >> 8); + replyData[2] = (byte)totalLength; if (head != null) { - mData.write(head); + System.arraycopy(head, 0, replyData, 3, head.length); } /* * Write the OBEX DISCONNECT packet to the server. Byte 0: response code * Byte 1&2: Connect Packet Length Byte 3 to n: headers */ - mData.read(mOutput); + mOutput.write(replyData); mOutput.flush(); } @@ -513,6 +525,7 @@ public final class ServerSession extends ObexSession implements Runnable { int code = -1; HeaderSet request = new HeaderSet(); HeaderSet reply = new HeaderSet(); + int bytesReceived; /* * Read in the length of the OBEX packet, OBEX version, flags, and max @@ -535,10 +548,15 @@ public final class ServerSession extends ObexSession implements Runnable { totalLength = 7; } else { if (packetLength > 7) { - mData.reset(); - mData.write(mInput, packetLength - 7); + byte[] headers = new byte[packetLength - 7]; + bytesReceived = mInput.read(headers); + + while (bytesReceived != headers.length) { + bytesReceived += mInput.read(headers, bytesReceived, headers.length + - bytesReceived); + } - ObexHelper.updateHeaderSet(request, mData, null, mHeaderBuffer); + ObexHelper.updateHeaderSet(request, headers); } if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { |