diff options
Diffstat (limited to 'obex/javax/obex/ObexHelper.java')
-rw-r--r-- | obex/javax/obex/ObexHelper.java | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/obex/javax/obex/ObexHelper.java b/obex/javax/obex/ObexHelper.java index 1b66662..afb8101 100644 --- a/obex/javax/obex/ObexHelper.java +++ b/obex/javax/obex/ObexHelper.java @@ -151,21 +151,22 @@ 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 - * @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 + * @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(). * @throws IOException if an invalid header was found */ - public static byte[] updateHeaderSet(HeaderSet header, byte[] headerArray) throws IOException { + static void updateHeaderSet(HeaderSet header, ObexByteBuffer headerArray, + ObexByteBuffer bodyBuffer, ObexByteBuffer headerBuffer) throws IOException { int index = 0; int length = 0; int headerID; byte[] value = null; - byte[] body = null; HeaderSet headerImpl = header; try { - while (index < headerArray.length) { - headerID = 0xFF & headerArray[index]; + while (index < headerArray.getLength()) { + headerID = 0xFF & headerArray.peek(index); switch (headerID & (0xC0)) { /* @@ -181,14 +182,15 @@ public final class ObexHelper { case 0x40: boolean trimTail = true; index++; - length = 0xFF & headerArray[index]; + length = 0xFF & headerArray.peek(index); length = length << 8; index++; - length += 0xFF & headerArray[index]; + length += 0xFF & headerArray.peek(index); length -= 3; index++; - value = new byte[length]; - System.arraycopy(headerArray, index, value, 0, length); + headerBuffer.reset(); + headerBuffer.write(headerArray, index, length); + value = headerBuffer.getBytes(); if (length == 0 || (length > 0 && (value[length - 1] != 0))) { trimTail = false; } @@ -198,10 +200,10 @@ public final class ObexHelper { // Remove trailing null if (trimTail == false) { headerImpl.setHeader(headerID, new String(value, 0, - value.length, "ISO8859_1")); + length, "ISO8859_1")); } else { headerImpl.setHeader(headerID, new String(value, 0, - value.length - 1, "ISO8859_1")); + length - 1, "ISO8859_1")); } } catch (UnsupportedEncodingException e) { throw e; @@ -210,27 +212,27 @@ public final class ObexHelper { case HeaderSet.AUTH_CHALLENGE: headerImpl.mAuthChall = new byte[length]; - System.arraycopy(headerArray, index, headerImpl.mAuthChall, 0, - length); + headerArray.peek(index, headerImpl.mAuthChall); break; case HeaderSet.AUTH_RESPONSE: headerImpl.mAuthResp = new byte[length]; - System.arraycopy(headerArray, index, headerImpl.mAuthResp, 0, - length); + headerArray.peek(index, headerImpl.mAuthResp); break; case HeaderSet.BODY: /* Fall Through */ case HeaderSet.END_OF_BODY: - body = new byte[length + 1]; - body[0] = (byte)headerID; - System.arraycopy(headerArray, index, body, 1, length); + if (bodyBuffer != null) { + bodyBuffer.reset(); + bodyBuffer.write((byte)headerID); + bodyBuffer.write(headerArray, index, length); + } break; case HeaderSet.TIME_ISO_8601: try { - String dateString = new String(value, "ISO8859_1"); + String dateString = new String(value, 0, length, "ISO8859_1"); Calendar temp = Calendar.getInstance(); if ((dateString.length() == 16) && (dateString.charAt(15) == 'Z')) { @@ -257,7 +259,7 @@ public final class ObexHelper { default: if ((headerID & 0xC0) == 0x00) { headerImpl.setHeader(headerID, ObexHelper.convertToUnicode( - value, true)); + value, length, true)); } else { headerImpl.setHeader(headerID, value); } @@ -273,7 +275,7 @@ public final class ObexHelper { case 0x80: index++; try { - headerImpl.setHeader(headerID, Byte.valueOf(headerArray[index])); + headerImpl.setHeader(headerID, Byte.valueOf(headerArray.peek(index))); } catch (Exception e) { // Not a valid header so ignore } @@ -288,7 +290,7 @@ public final class ObexHelper { case 0xC0: index++; value = new byte[4]; - System.arraycopy(headerArray, index, value, 0, 4); + headerArray.peek(index, value); try { if (headerID != HeaderSet.TIME_4_BYTE) { // Determine if it is a connection ID. These @@ -317,8 +319,6 @@ 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, boolean includesNull) { - if (b == null || b.length == 0) { + public static String convertToUnicode(byte[] b, int len, boolean includesNull) { + if (b == null || len == 0) { return null; } - int arrayLength = b.length; + int arrayLength = len; if (!((arrayLength % 2) == 0)) { throw new IllegalArgumentException("Byte array not of a valid form"); } |