19439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/*
29439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc.
39439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
49439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * All rights reserved.
59439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
69439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Redistribution and use in source and binary forms, with or without
79439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * modification, are permitted provided that the following conditions are met:
89439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
99439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions of source code must retain the above copyright notice,
109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer.
119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice,
139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer in the documentation
149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * and/or other materials provided with the distribution.
159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors
179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * may be used to endorse or promote products derived from this software
189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * without specific prior written permission.
199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * POSSIBILITY OF SUCH DAMAGE.
319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellypackage javax.obex;
349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.IOException;
369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.InputStream;
379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.OutputStream;
389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.DataInputStream;
399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.DataOutputStream;
409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellyimport java.io.ByteArrayOutputStream;
419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/**
4305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * This class implements the <code>Operation</code> interface. It will read and
4405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * write data via puts and gets.
452e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly * @hide
469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
472e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellypublic final class ClientOperation implements Operation, BaseStream {
489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private ClientSession mParent;
509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mInputOpen;
529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private PrivateInputStream mPrivateInput;
549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mPrivateInputOpen;
569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private PrivateOutputStream mPrivateOutput;
589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mPrivateOutputOpen;
609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mExceptionMessage;
629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private int mMaxPacketSize;
649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mOperationDone;
669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mGetOperation;
689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private HeaderSet mRequestHeader;
709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private HeaderSet mReplyHeader;
729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mEndOfBodySent;
749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    /**
769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Creates new OperationImpl to read and write data to a server
779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param maxSize the maximum packet size
789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param p the parent to this object
799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param type <code>true</code> if this is a get request;
8005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        <code>false</code. if this is a put request
8105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param header the header to set in the initial request
8205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if the an IO error occurred
839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public ClientOperation(int maxSize, ClientSession p, HeaderSet header, boolean type)
853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throws IOException {
869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mParent = p;
883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mEndOfBodySent = false;
893998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInputOpen = true;
903998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOperationDone = false;
913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketSize = maxSize;
923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mGetOperation = type;
939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInputOpen = false;
953998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutputOpen = false;
963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInput = null;
973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutput = null;
989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mReplyHeader = new HeaderSet();
1009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mRequestHeader = new HeaderSet();
1029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int[] headerList = header.getHeaderList();
1049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (headerList != null) {
1069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            for (int i = 0; i < headerList.length; i++) {
1083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mRequestHeader.setHeader(headerList[i], header.getHeader(headerList[i]));
1099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((header).mAuthChall != null) {
1133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mRequestHeader.mAuthChall = new byte[(header).mAuthChall.length];
1143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            System.arraycopy((header).mAuthChall, 0, mRequestHeader.mAuthChall, 0,
1153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    (header).mAuthChall.length);
1169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((header).mAuthResp != null) {
1193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mRequestHeader.mAuthResp = new byte[(header).mAuthResp.length];
1203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            System.arraycopy((header).mAuthResp, 0, mRequestHeader.mAuthResp, 0,
1213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    (header).mAuthResp.length);
1229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1240d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz
1250d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz        if ((header).mConnectionID != null) {
1260d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz            mRequestHeader.mConnectionID = new byte[4];
1270d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz            System.arraycopy((header).mConnectionID, 0, mRequestHeader.mConnectionID, 0,
1280d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz                    4);
1290d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz
1300d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz        }
1319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
13405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sends an ABORT message to the server. By calling this method, the
1359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * corresponding input and output streams will be closed along with this
1369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * object.
13705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if the transaction has already ended or if an OBEX
13805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         server called this method
1399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized void abort() throws IOException {
1419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
1429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        //no compatible with sun-ri
1433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mOperationDone) && (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE)) {
1449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already ended");
1459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mExceptionMessage = "Operation aborted";
1483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((!mOperationDone) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
1493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mOperationDone = true;
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
1519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * Since we are not sending any headers or returning any headers then
1529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * we just need to write and read the same bytes
1539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
1543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mParent.sendRequest(ObexHelper.OBEX_OPCODE_ABORT, null, mReplyHeader, null);
1559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_OK) {
1579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IOException("Invalid response code from server");
1589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mExceptionMessage = null;
1619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        close();
1649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
16705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Retrieves the response code retrieved from the server. Response codes are
16805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * defined in the <code>ResponseCodes</code> interface.
1699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the response code retrieved from the server
1702e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
17105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the transaction; if this method is called on a
17205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object created by calling
17305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>createHeaderSet</code> in a <code>ClientSession</code>
17405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         object
1759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized int getResponseCode() throws IOException {
1779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        //avoid dup validateConnection
1783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mReplyHeader.responseCode == -1)
1793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                || (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
1809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            validateConnection();
1819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mReplyHeader.responseCode;
1849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * This method will always return <code>null</code>
1889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return <code>null</code>
1899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public String getEncoding() {
1919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return null;
1929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the type of content that the resource connected to is providing.
1969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * E.g. if the connection is via HTTP, then the value of the content-type
1979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * header field is returned.
1989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the content type of the resource that the URL references, or
19905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>null</code> if not known
2009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public String getType() {
2029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
2033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            return (String)mReplyHeader.getHeader(HeaderSet.TYPE);
2049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (IOException e) {
2059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return null;
2069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the length of the content which is being provided. E.g. if the
21105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * connection is via HTTP, then the value of the content-length header field
21205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * is returned.
2139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the content length of the resource that this connection's URL
21405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         references, or -1 if the content length is not known
2159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public long getLength() {
2179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
2183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Long temp = (Long)mReplyHeader.getHeader(HeaderSet.LENGTH);
2199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (temp == null) {
2219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return -1;
2229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
2239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return temp.longValue();
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (IOException e) {
2269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return -1;
2279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return an input stream for a connection.
2329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an input stream
2332e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public InputStream openInputStream() throws IOException {
2369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
2389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInputOpen)
2409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("no more input streams available");
2413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
2429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // send the GET request here
2439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            validateConnection();
2449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
2453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mPrivateInput == null) {
2463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mPrivateInput = new PrivateInputStream(this);
2479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInputOpen = true;
2519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mPrivateInput;
2539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
25505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    /**
2569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return a data input stream for a connection.
2579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an input stream
2582e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public DataInputStream openDataInputStream() throws IOException {
2619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return new DataInputStream(openInputStream());
2629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return an output stream for a connection.
2669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an output stream
2672e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public OutputStream openOutputStream() throws IOException {
2703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
2729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureNotDone();
2739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutputOpen)
2759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("no more output streams available");
2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutput == null) {
2789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // there are 3 bytes operation headers and 3 bytes body headers //
27965208317ba9d16486b47ebcaa868c596d424c87fLixin Yue            mPrivateOutput = new PrivateOutputStream(this, getMaxPacketSize());
2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutputOpen = true;
2839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mPrivateOutput;
2859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int getMaxPacketSize() {
28865208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        return mMaxPacketSize - 6 - getHeaderLength();
28965208317ba9d16486b47ebcaa868c596d424c87fLixin Yue    }
29065208317ba9d16486b47ebcaa868c596d424c87fLixin Yue
29165208317ba9d16486b47ebcaa868c596d424c87fLixin Yue    public int getHeaderLength() {
29265208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        // OPP may need it
29365208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
29465208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        return headerArray.length;
2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return a data output stream for a connection.
2999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an output stream
3002e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
3019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public DataOutputStream openDataOutputStream() throws IOException {
3039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return new DataOutputStream(openOutputStream());
3049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Closes the connection and ends the transaction
3082e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if the operation has already ended or is closed
3099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void close() throws IOException {
3113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInputOpen = false;
3123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInputOpen = false;
3133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutputOpen = false;
3143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mParent.setRequestInactive();
3159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the headers that have been received during the operation.
31905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Modifying the object returned has no effect on the headers that are sent
32005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * or retrieved.
3219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the headers received during this <code>Operation</code>
3222e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if this <code>Operation</code> has been closed
3239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public HeaderSet getReceivedHeader() throws IOException {
3259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mReplyHeader;
3289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Specifies the headers that should be sent in the next OBEX message that
3329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * is sent.
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headers the headers to send in the next message
33405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if this <code>Operation</code> has been closed or the
33505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         transaction has ended and no further messages will be exchanged
3362e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IllegalArgumentException if <code>headers</code> was not created
33705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         by a call to <code>ServerRequestHandler.createHeaderSet()</code>
3382e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws NullPointerException if <code>headers</code> is <code>null</code>
3399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void sendHeaders(HeaderSet headers) throws IOException {
3419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mOperationDone) {
3439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already exchanged all data");
3449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (headers == null) {
3473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throw new IOException("Headers may not be null");
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int[] headerList = headers.getHeaderList();
3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (headerList != null) {
3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            for (int i = 0; i < headerList.length; i++) {
3533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mRequestHeader.setHeader(headerList[i], headers.getHeader(headerList[i]));
3549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
35905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Verifies that additional information may be sent. In other words, the
3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * operation is not done.
3612e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if the operation is completed
3629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void ensureNotDone() throws IOException {
3643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mOperationDone) {
3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has completed");
3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the connection is open and no exceptions should be thrown.
3712e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an exception needs to be thrown
3729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void ensureOpen() throws IOException {
3743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mParent.ensureOpen();
3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mExceptionMessage != null) {
3773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throw new IOException(mExceptionMessage);
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (!mInputOpen) {
3809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already ended");
3819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the connection is open and the proper data has been read.
3862e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
3879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void validateConnection() throws IOException {
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // to sure only one privateInput object exist.
3923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInput == null) {
3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            startProcessing();
3949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Sends a request to the client of the specified type
39905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param opCode the request code to send to the client
4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return <code>true</code> if there is more data to send;
40105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>false</code> if there is no more data to send
4022e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
40405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    private boolean sendRequest(int opCode) throws IOException {
4059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        boolean returnValue = false;
4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ByteArrayOutputStream out = new ByteArrayOutputStream();
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bodyLength = -1;
4083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        byte[] headerArray = ObexHelper.createHeader(mRequestHeader, true);
4093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutput != null) {
4103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            bodyLength = mPrivateOutput.size();
4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
4149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Determine if there is space to add a body request.  At present
4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * this method checks to see if there is room for at least a 17
4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * byte body header.  This number needs to be at least 6 so that
4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * there is room for the header ID and length and the reply ID and
4189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * length, but it is a waste of resources if we can't send much of
4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * the body.
4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
4213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((ObexHelper.BASE_PACKET_LENGTH + headerArray.length) > mMaxPacketSize) {
4229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int end = 0;
4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int start = 0;
4249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // split & send the headerArray in multiple packets.
4259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            while (end != headerArray.length) {
4279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                //split the headerArray
4283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                end = ObexHelper.findHeaderEnd(headerArray, start, mMaxPacketSize
4293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        - ObexHelper.BASE_PACKET_LENGTH);
4300d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz                // can not split
4319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (end == -1) {
4323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
4339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    abort();
4343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mExceptionMessage = "Header larger then can be sent in a packet";
4353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mInputOpen = false;
4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (mPrivateInput != null) {
4383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mPrivateInput.close();
4399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (mPrivateOutput != null) {
4423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mPrivateOutput.close();
4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IOException("OBEX Packet exceeds max packet size");
4459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] sendHeader = new byte[end - start];
4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                System.arraycopy(headerArray, start, sendHeader, 0, sendHeader.length);
44905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                if (!mParent.sendRequest(opCode, sendHeader, mReplyHeader, mPrivateInput)) {
4509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return false;
4519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
4549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return false;
4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                start = end;
4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (bodyLength > 0) {
4619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
4629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
4649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(headerArray);
4679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (bodyLength > 0) {
4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
4713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun             * Determine if we can send the whole body or just part of
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * the body.  Remember that there is the 3 bytes for the
4739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * response message and 3 bytes for the header ID and length
4749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
4753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (bodyLength > (mMaxPacketSize - headerArray.length - 6)) {
4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                returnValue = true;
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bodyLength = mMaxPacketSize - headerArray.length - 6;
4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            byte[] body = mPrivateOutput.readBytes(bodyLength);
4829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
4849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * Since this is a put request if the final bit is set or
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * the output stream is closed we need to send the 0x49
4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * (End of Body) otherwise, we need to send 0x48 (Body)
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
4883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((mPrivateOutput.isClosed()) && (!returnValue) && (!mEndOfBodySent)
48905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                    && ((opCode & 0x80) != 0)) {
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x49);
4913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mEndOfBodySent = true;
4929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x48);
4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            bodyLength += 3;
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)(bodyLength >> 8));
4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)bodyLength);
4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (body != null) {
5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(body);
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutputOpen && bodyLength <= 0 && !mEndOfBodySent) {
5069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // only 0x82 or 0x83 can send 0x49
50705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            if ((opCode & 0x80) == 0) {
5089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x48);
5099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x49);
5113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mEndOfBodySent = true;
5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            bodyLength = 3;
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)(bodyLength >> 8));
5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)bodyLength);
5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (out.size() == 0) {
52105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            if (!mParent.sendRequest(opCode, null, mReplyHeader, mPrivateInput)) {
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
5239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return returnValue;
5259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((out.size() > 0)
52705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                && (!mParent.sendRequest(opCode, out.toByteArray(), mReplyHeader, mPrivateInput))) {
5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return false;
5299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5310d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz        // send all of the output data in 0x48,
5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // send 0x49 with empty body
5333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mPrivateOutput != null) && (mPrivateOutput.size() > 0))
5349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            returnValue = true;
5359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return returnValue;
5379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
54005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * This method starts the processing thread results. It will send the
54105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * initial request. If the response takes more then one packet, a thread
5429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * will be started to handle additional requests
5432e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private synchronized void startProcessing() throws IOException {
5469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInput == null) {
5483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mPrivateInput = new PrivateInputStream(this);
5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        boolean more = true;
5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
5533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!mOperationDone) {
5543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
5553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
5569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x03);
5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
5603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
5619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
5633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
5679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!mOperationDone) {
5693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
5703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
5719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x02);
5729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
5773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mParent.sendRequest(0x82, null, mReplyHeader, mPrivateInput);
5789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
5813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
5829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Continues the operation since there is no data to read.
58805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param sendEmpty <code>true</code> if the operation should send an empty
58905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        packet or not send anything if there is no data to send
59005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param inStream <code>true</code> if the stream is input stream or is
59105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        output stream
5922e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
5939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized boolean continueOperation(boolean sendEmpty, boolean inStream)
5959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throws IOException {
5969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
5983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((inStream) && (!mOperationDone)) {
5999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with inputstream in get operation
6003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
6019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                /*
6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                  * Determine if that was not the last packet in the operation
6039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                  */
6043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
6053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
6069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((!inStream) && (!mOperationDone)) {
6119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in get operation
6129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mPrivateInput == null) {
6143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mPrivateInput = new PrivateInputStream(this);
6159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x03);
6179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (mOperationDone) {
6209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
6243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((!inStream) && (!mOperationDone)) {
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in put operation
6263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x02);
6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((inStream) && (!mOperationDone)) {
6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // How to deal with inputstream  in put operation ?
6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (mOperationDone) {
6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return false;
6419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Called when the output or input stream is closed.
6459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param inStream <code>true</code> if the input stream is closed;
64605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        <code>false</code> if the output stream is closed
6472e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
6489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void streamClosed(boolean inStream) throws IOException {
6503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (!mGetOperation) {
6513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((!inStream) && (!mOperationDone)) {
6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in put operation
6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean more = true;
6559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0)) {
6573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
6589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerArray.length <= 0)
6599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        more = false;
6609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // If have not sent any data so send  all now
6623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
6679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x02);
6689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                /*
6719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * According to the IrOBEX specification, after the final put, you
6729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * only have a single reply to send.  so we don't need the while
6739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * loop.
6749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 */
6753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
6769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendRequest(0x82);
6789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
6803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((inStream) && (mOperationDone)) {
6819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // how to deal with input stream in put stream ?
6823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
6839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
6853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((inStream) && (!mOperationDone)) {
6869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with inputstream in get operation
6889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Have not sent any data so send it all now
6899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6903998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
6959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!sendRequest(0x83)) {
6969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
6979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
6989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
7003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
7019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
7033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((!inStream) && (!mOperationDone)) {
7049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in get operation
7059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // part of the data may have been sent in continueOperation.
7069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean more = true;
7089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0)) {
7103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
7119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerArray.length <= 0)
7129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        more = false;
7139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mPrivateInput == null) {
7163998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mPrivateInput = new PrivateInputStream(this);
7179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0))
7199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = false;
7209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
7223998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
7239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x03);
7249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x83);
7269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                //                parent.sendRequest(0x83, null, replyHeaders, privateInput);
7273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
7283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
7299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
7319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
7329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
733fe3807a5b23f54f6539436d71aa0cd931a2b76f0Matthew Xie
734fe3807a5b23f54f6539436d71aa0cd931a2b76f0Matthew Xie    public void noBodyHeader(){
735fe3807a5b23f54f6539436d71aa0cd931a2b76f0Matthew Xie    }
7369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
737