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        }
1249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
12705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sends an ABORT message to the server. By calling this method, the
1289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * corresponding input and output streams will be closed along with this
1299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * object.
13005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if the transaction has already ended or if an OBEX
13105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         server called this method
1329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized void abort() throws IOException {
1349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
1359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        //no compatible with sun-ri
1363998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mOperationDone) && (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE)) {
1379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already ended");
1389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mExceptionMessage = "Operation aborted";
1413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((!mOperationDone) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
1423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mOperationDone = true;
1439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
1449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * Since we are not sending any headers or returning any headers then
1459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * we just need to write and read the same bytes
1469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
1473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mParent.sendRequest(ObexHelper.OBEX_OPCODE_ABORT, null, mReplyHeader, null);
1489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_OK) {
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IOException("Invalid response code from server");
1519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mExceptionMessage = null;
1549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        close();
1579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
16005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Retrieves the response code retrieved from the server. Response codes are
16105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * defined in the <code>ResponseCodes</code> interface.
1629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the response code retrieved from the server
1632e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
16405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the transaction; if this method is called on a
16505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object created by calling
16605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>createHeaderSet</code> in a <code>ClientSession</code>
16705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         object
1689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized int getResponseCode() throws IOException {
1709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        //avoid dup validateConnection
1713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mReplyHeader.responseCode == -1)
1723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                || (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
1739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            validateConnection();
1749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mReplyHeader.responseCode;
1779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * This method will always return <code>null</code>
1819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return <code>null</code>
1829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public String getEncoding() {
1849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return null;
1859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the type of content that the resource connected to is providing.
1899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * E.g. if the connection is via HTTP, then the value of the content-type
1909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * header field is returned.
1919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the content type of the resource that the URL references, or
19205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>null</code> if not known
1939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public String getType() {
1959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
1963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            return (String)mReplyHeader.getHeader(HeaderSet.TYPE);
1979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (IOException e) {
1989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return null;
1999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the length of the content which is being provided. E.g. if the
20405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * connection is via HTTP, then the value of the content-length header field
20505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * is returned.
2069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the content length of the resource that this connection's URL
20705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         references, or -1 if the content length is not known
2089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public long getLength() {
2109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
2113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Long temp = (Long)mReplyHeader.getHeader(HeaderSet.LENGTH);
2129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (temp == null) {
2149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return -1;
2159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
2169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return temp.longValue();
2179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (IOException e) {
2199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return -1;
2209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return an input stream for a connection.
2259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an input stream
2262e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public InputStream openInputStream() throws IOException {
2299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
2319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInputOpen)
2339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("no more input streams available");
2343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
2359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // send the GET request here
2369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            validateConnection();
2379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
2383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mPrivateInput == null) {
2393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mPrivateInput = new PrivateInputStream(this);
2409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInputOpen = true;
2449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mPrivateInput;
2469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
24805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    /**
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return a data input stream for a connection.
2509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an input stream
2512e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public DataInputStream openDataInputStream() throws IOException {
2549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return new DataInputStream(openInputStream());
2559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return an output stream for a connection.
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an output stream
2602e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public OutputStream openOutputStream() throws IOException {
2633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureNotDone();
2669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutputOpen)
2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("no more output streams available");
2699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutput == null) {
2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // there are 3 bytes operation headers and 3 bytes body headers //
27265208317ba9d16486b47ebcaa868c596d424c87fLixin Yue            mPrivateOutput = new PrivateOutputStream(this, getMaxPacketSize());
2739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutputOpen = true;
2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mPrivateOutput;
2789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int getMaxPacketSize() {
28165208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        return mMaxPacketSize - 6 - getHeaderLength();
28265208317ba9d16486b47ebcaa868c596d424c87fLixin Yue    }
28365208317ba9d16486b47ebcaa868c596d424c87fLixin Yue
28465208317ba9d16486b47ebcaa868c596d424c87fLixin Yue    public int getHeaderLength() {
28565208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        // OPP may need it
28665208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
28765208317ba9d16486b47ebcaa868c596d424c87fLixin Yue        return headerArray.length;
2889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Open and return a data output stream for a connection.
2929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return an output stream
2932e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an I/O error occurs
2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public DataOutputStream openDataOutputStream() throws IOException {
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return new DataOutputStream(openOutputStream());
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Closes the connection and ends the transaction
3012e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if the operation has already ended or is closed
3029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void close() throws IOException {
3043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInputOpen = false;
3053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateInputOpen = false;
3063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mPrivateOutputOpen = false;
3073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mParent.setRequestInactive();
3089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Returns the headers that have been received during the operation.
31205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Modifying the object returned has no effect on the headers that are sent
31305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * or retrieved.
3149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the headers received during this <code>Operation</code>
3152e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if this <code>Operation</code> has been closed
3169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3173998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public HeaderSet getReceivedHeader() throws IOException {
3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        return mReplyHeader;
3219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Specifies the headers that should be sent in the next OBEX message that
3259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * is sent.
3269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headers the headers to send in the next message
32705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if this <code>Operation</code> has been closed or the
32805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         transaction has ended and no further messages will be exchanged
3292e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IllegalArgumentException if <code>headers</code> was not created
33005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         by a call to <code>ServerRequestHandler.createHeaderSet()</code>
3312e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws NullPointerException if <code>headers</code> is <code>null</code>
3329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void sendHeaders(HeaderSet headers) throws IOException {
3349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mOperationDone) {
3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already exchanged all data");
3379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (headers == null) {
3403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throw new IOException("Headers may not be null");
3419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int[] headerList = headers.getHeaderList();
3449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (headerList != null) {
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            for (int i = 0; i < headerList.length; i++) {
3463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mRequestHeader.setHeader(headerList[i], headers.getHeader(headerList[i]));
3479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
35205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Verifies that additional information may be sent. In other words, the
3539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * operation is not done.
3542e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if the operation is completed
3559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void ensureNotDone() throws IOException {
3573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mOperationDone) {
3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has completed");
3599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the connection is open and no exceptions should be thrown.
3642e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an exception needs to be thrown
3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void ensureOpen() throws IOException {
3673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mParent.ensureOpen();
3689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mExceptionMessage != null) {
3703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throw new IOException(mExceptionMessage);
3719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (!mInputOpen) {
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("Operation has already ended");
3749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the connection is open and the proper data has been read.
3792e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
3809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
3819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void validateConnection() throws IOException {
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ensureOpen();
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // to sure only one privateInput object exist.
3853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInput == null) {
3869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            startProcessing();
3879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Sends a request to the client of the specified type
39205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param opCode the request code to send to the client
3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return <code>true</code> if there is more data to send;
39405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>false</code> if there is no more data to send
3952e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
39705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    private boolean sendRequest(int opCode) throws IOException {
3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        boolean returnValue = false;
3999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ByteArrayOutputStream out = new ByteArrayOutputStream();
4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bodyLength = -1;
4013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        byte[] headerArray = ObexHelper.createHeader(mRequestHeader, true);
4023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutput != null) {
4033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            bodyLength = mPrivateOutput.size();
4049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Determine if there is space to add a body request.  At present
4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * this method checks to see if there is room for at least a 17
4099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * byte body header.  This number needs to be at least 6 so that
4109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * there is room for the header ID and length and the reply ID and
4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * length, but it is a waste of resources if we can't send much of
4129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * the body.
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
4143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((ObexHelper.BASE_PACKET_LENGTH + headerArray.length) > mMaxPacketSize) {
4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int end = 0;
4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int start = 0;
4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // split & send the headerArray in multiple packets.
4189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            while (end != headerArray.length) {
4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                //split the headerArray
4213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                end = ObexHelper.findHeaderEnd(headerArray, start, mMaxPacketSize
4223998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        - ObexHelper.BASE_PACKET_LENGTH);
4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // can not split
4249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (end == -1) {
4253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
4269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    abort();
4273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mExceptionMessage = "Header larger then can be sent in a packet";
4283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mInputOpen = false;
4299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (mPrivateInput != null) {
4313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mPrivateInput.close();
4329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (mPrivateOutput != null) {
4353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mPrivateOutput.close();
4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IOException("OBEX Packet exceeds max packet size");
4389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] sendHeader = new byte[end - start];
4419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                System.arraycopy(headerArray, start, sendHeader, 0, sendHeader.length);
44205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                if (!mParent.sendRequest(opCode, sendHeader, mReplyHeader, mPrivateInput)) {
4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return false;
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
4479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return false;
4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                start = end;
4519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (bodyLength > 0) {
4549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
4579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(headerArray);
4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (bodyLength > 0) {
4639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
4643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun             * Determine if we can send the whole body or just part of
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * the body.  Remember that there is the 3 bytes for the
4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * response message and 3 bytes for the header ID and length
4679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
4683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (bodyLength > (mMaxPacketSize - headerArray.length - 6)) {
4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                returnValue = true;
4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bodyLength = mMaxPacketSize - headerArray.length - 6;
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            byte[] body = mPrivateOutput.readBytes(bodyLength);
4759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            /*
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * Since this is a put request if the final bit is set or
4789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * the output stream is closed we need to send the 0x49
4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             * (End of Body) otherwise, we need to send 0x48 (Body)
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly             */
4813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((mPrivateOutput.isClosed()) && (!returnValue) && (!mEndOfBodySent)
48205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                    && ((opCode & 0x80) != 0)) {
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x49);
4843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mEndOfBodySent = true;
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x48);
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            bodyLength += 3;
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)(bodyLength >> 8));
4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)bodyLength);
4929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (body != null) {
4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(body);
4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateOutputOpen && bodyLength <= 0 && !mEndOfBodySent) {
4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // only 0x82 or 0x83 can send 0x49
50005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            if ((opCode & 0x80) == 0) {
5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x48);
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(0x49);
5043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mEndOfBodySent = true;
5059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            bodyLength = 3;
5099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)(bodyLength >> 8));
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write((byte)bodyLength);
5119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (out.size() == 0) {
51405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            if (!mParent.sendRequest(opCode, null, mReplyHeader, mPrivateInput)) {
5159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return returnValue;
5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((out.size() > 0)
52005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                && (!mParent.sendRequest(opCode, out.toByteArray(), mReplyHeader, mPrivateInput))) {
5219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return false;
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // send all of the output data in 0x48,
5259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // send 0x49 with empty body
5263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if ((mPrivateOutput != null) && (mPrivateOutput.size() > 0))
5279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            returnValue = true;
5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return returnValue;
5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
53305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * This method starts the processing thread results. It will send the
53405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * initial request. If the response takes more then one packet, a thread
5359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * will be started to handle additional requests
5362e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
5379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private synchronized void startProcessing() throws IOException {
5399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mPrivateInput == null) {
5413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mPrivateInput = new PrivateInputStream(this);
5429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        boolean more = true;
5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
5463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!mOperationDone) {
5473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
5483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x03);
5509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
5533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
5563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!mOperationDone) {
5623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
5633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x02);
5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
5703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mParent.sendRequest(0x82, null, mReplyHeader, mPrivateInput);
5719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
5743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Continues the operation since there is no data to read.
58105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param sendEmpty <code>true</code> if the operation should send an empty
58205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        packet or not send anything if there is no data to send
58305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param inStream <code>true</code> if the stream is input stream or is
58405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        output stream
5852e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
5869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized boolean continueOperation(boolean sendEmpty, boolean inStream)
5889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throws IOException {
5899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5903998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mGetOperation) {
5913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((inStream) && (!mOperationDone)) {
5929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with inputstream in get operation
5933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
5949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                /*
5959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                  * Determine if that was not the last packet in the operation
5969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                  */
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
5983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
5999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((!inStream) && (!mOperationDone)) {
6049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in get operation
6059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mPrivateInput == null) {
6073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mPrivateInput = new PrivateInputStream(this);
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x03);
6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (mOperationDone) {
6139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
6173998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((!inStream) && (!mOperationDone)) {
6189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in put operation
6193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x02);
6239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return true;
6243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((inStream) && (!mOperationDone)) {
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // How to deal with inputstream  in put operation ?
6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (mOperationDone) {
6299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                return false;
6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return false;
6349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Called when the output or input stream is closed.
6389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param inStream <code>true</code> if the input stream is closed;
63905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        <code>false</code> if the output stream is closed
6402e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an IO error occurs
6419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void streamClosed(boolean inStream) throws IOException {
6433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (!mGetOperation) {
6443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((!inStream) && (!mOperationDone)) {
6459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in put operation
6469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean more = true;
6489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0)) {
6503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
6519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerArray.length <= 0)
6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        more = false;
6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // If have not sent any data so send  all now
6553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
6609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x02);
6619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                /*
6649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * According to the IrOBEX specification, after the final put, you
6659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * only have a single reply to send.  so we don't need the while
6669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 * loop.
6679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                 */
6683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
6699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendRequest(0x82);
6719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
6733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((inStream) && (mOperationDone)) {
6749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // how to deal with input stream in put stream ?
6753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
6769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
6783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((inStream) && (!mOperationDone)) {
6799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with inputstream in get operation
6819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Have not sent any data so send it all now
6829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode == -1) {
6843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
6859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
6889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!sendRequest(0x83)) {
6899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
6909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
6919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE) {
6933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mParent.sendRequest(0x83, null, mReplyHeader, mPrivateInput);
6949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6953998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mOperationDone = true;
6963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if ((!inStream) && (!mOperationDone)) {
6979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // to deal with outputstream in get operation
6989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // part of the data may have been sent in continueOperation.
6999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean more = true;
7019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0)) {
7033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    byte[] headerArray = ObexHelper.createHeader(mRequestHeader, false);
7049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerArray.length <= 0)
7059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        more = false;
7069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mPrivateInput == null) {
7093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mPrivateInput = new PrivateInputStream(this);
7109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if ((mPrivateOutput != null) && (mPrivateOutput.size() <= 0))
7129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = false;
7139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mReplyHeader.responseCode = ResponseCodes.OBEX_HTTP_CONTINUE;
7153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while ((more) && (mReplyHeader.responseCode == ResponseCodes.OBEX_HTTP_CONTINUE)) {
7169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    more = sendRequest(0x03);
7179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                sendRequest(0x83);
7199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                //                parent.sendRequest(0x83, null, replyHeaders, privateInput);
7203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (mReplyHeader.responseCode != ResponseCodes.OBEX_HTTP_CONTINUE) {
7213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mOperationDone = true;
7229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
7239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
7249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
7259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
7269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
727