blob: 5daee72474fc40434b957a13824eb3989e8d239f [file] [log] [blame]
Nick Pelly9439a7f2009-06-30 12:04:36 -07001/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package javax.obex;
34
Nick Pelly2e0da962009-06-30 16:28:54 -070035import java.io.InputStream;
36import java.io.IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -070037
38/**
39 * This object provides an input stream to the Operation objects used in this
40 * package.
Nick Pelly2e0da962009-06-30 16:28:54 -070041 * @hide
Nick Pelly9439a7f2009-06-30 12:04:36 -070042 */
Tao Liejun3998bf02009-07-02 19:29:09 +080043public final class PrivateInputStream extends InputStream {
Nick Pelly9439a7f2009-06-30 12:04:36 -070044
Tao Liejun3998bf02009-07-02 19:29:09 +080045 private BaseStream mParent;
Nick Pelly9439a7f2009-06-30 12:04:36 -070046
Tao Liejun3998bf02009-07-02 19:29:09 +080047 private byte[] mData;
Nick Pelly9439a7f2009-06-30 12:04:36 -070048
Tao Liejun3998bf02009-07-02 19:29:09 +080049 private int mIndex;
Nick Pelly9439a7f2009-06-30 12:04:36 -070050
Tao Liejun3998bf02009-07-02 19:29:09 +080051 private boolean mOpen;
Nick Pelly9439a7f2009-06-30 12:04:36 -070052
53 /**
54 * Creates an input stream for the <code>Operation</code> to read from
Nick Pelly9439a7f2009-06-30 12:04:36 -070055 * @param p the connection this input stream is for
56 */
57 public PrivateInputStream(BaseStream p) {
Tao Liejun3998bf02009-07-02 19:29:09 +080058 mParent = p;
59 mData = new byte[0];
60 mIndex = 0;
61 mOpen = true;
Nick Pelly9439a7f2009-06-30 12:04:36 -070062 }
63
64 /**
65 * Returns the number of bytes that can be read (or skipped over) from this
66 * input stream without blocking by the next caller of a method for this
67 * input stream. The next caller might be the same thread or or another
68 * thread.
Nick Pelly9439a7f2009-06-30 12:04:36 -070069 * @return the number of bytes that can be read from this input stream
Tao Liejun05ff98bb2009-07-13 15:57:11 -070070 * without blocking
Nick Pelly2e0da962009-06-30 16:28:54 -070071 * @throws IOException if an I/O error occurs
Nick Pelly9439a7f2009-06-30 12:04:36 -070072 */
73 @Override
74 public synchronized int available() throws IOException {
75 ensureOpen();
Tao Liejun3998bf02009-07-02 19:29:09 +080076 return mData.length - mIndex;
Nick Pelly9439a7f2009-06-30 12:04:36 -070077 }
78
79 /**
80 * Reads the next byte of data from the input stream. The value byte is
Tao Liejun05ff98bb2009-07-13 15:57:11 -070081 * returned as an int in the range 0 to 255. If no byte is available because
82 * the end of the stream has been reached, the value -1 is returned. This
83 * method blocks until input data is available, the end of the stream is
84 * detected, or an exception is thrown.
85 * @return the byte read from the input stream or -1 if it reaches the end of
86 * stream
Nick Pelly2e0da962009-06-30 16:28:54 -070087 * @throws IOException if an I/O error occurs
Nick Pelly9439a7f2009-06-30 12:04:36 -070088 */
89 @Override
90 public synchronized int read() throws IOException {
91 ensureOpen();
Tao Liejun3998bf02009-07-02 19:29:09 +080092 while (mData.length == mIndex) {
93 if (!mParent.continueOperation(true, true)) {
Nick Pelly9439a7f2009-06-30 12:04:36 -070094 return -1;
95 }
96 }
Tao Liejun3998bf02009-07-02 19:29:09 +080097 return (mData[mIndex++] & 0xFF);
Nick Pelly9439a7f2009-06-30 12:04:36 -070098 }
99
100 @Override
101 public int read(byte[] b) throws IOException {
102 return read(b, 0, b.length);
103 }
104
105 @Override
106 public synchronized int read(byte[] b, int offset, int length) throws IOException {
107
108 if (b == null) {
Tao Liejun3998bf02009-07-02 19:29:09 +0800109 throw new IOException("buffer is null");
Nick Pelly9439a7f2009-06-30 12:04:36 -0700110 }
111 if ((offset | length) < 0 || length > b.length - offset) {
112 throw new ArrayIndexOutOfBoundsException("index outof bound");
113 }
114 ensureOpen();
115
Tao Liejun3998bf02009-07-02 19:29:09 +0800116 int currentDataLength = mData.length - mIndex;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700117 int remainReadLength = length;
118 int offset1 = offset;
119 int result = 0;
120
121 while (currentDataLength <= remainReadLength) {
Tao Liejun3998bf02009-07-02 19:29:09 +0800122 System.arraycopy(mData, mIndex, b, offset1, currentDataLength);
123 mIndex += currentDataLength;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700124 offset1 += currentDataLength;
125 result += currentDataLength;
126 remainReadLength -= currentDataLength;
127
Tao Liejun3998bf02009-07-02 19:29:09 +0800128 if (!mParent.continueOperation(true, true)) {
Nick Pelly9439a7f2009-06-30 12:04:36 -0700129 return result == 0 ? -1 : result;
130 }
Tao Liejun3998bf02009-07-02 19:29:09 +0800131 currentDataLength = mData.length - mIndex;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700132 }
133 if (remainReadLength > 0) {
Tao Liejun3998bf02009-07-02 19:29:09 +0800134 System.arraycopy(mData, mIndex, b, offset1, remainReadLength);
135 mIndex += remainReadLength;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700136 result += remainReadLength;
137 }
138 return result;
139 }
140
141 /**
142 * Allows the <code>OperationImpl</code> thread to add body data to the
143 * input stream.
Nick Pelly9439a7f2009-06-30 12:04:36 -0700144 * @param body the data to add to the stream
Nick Pelly9439a7f2009-06-30 12:04:36 -0700145 * @param start the start of the body to array to copy
146 */
147 public synchronized void writeBytes(byte[] body, int start) {
148
Tao Liejun3998bf02009-07-02 19:29:09 +0800149 int length = (body.length - start) + (mData.length - mIndex);
Nick Pelly9439a7f2009-06-30 12:04:36 -0700150 byte[] temp = new byte[length];
151
Tao Liejun3998bf02009-07-02 19:29:09 +0800152 System.arraycopy(mData, mIndex, temp, 0, mData.length - mIndex);
153 System.arraycopy(body, start, temp, mData.length - mIndex, body.length - start);
Nick Pelly9439a7f2009-06-30 12:04:36 -0700154
Tao Liejun3998bf02009-07-02 19:29:09 +0800155 mData = temp;
156 mIndex = 0;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700157 notifyAll();
158 }
159
160 /**
161 * Verifies that this stream is open
Nick Pelly2e0da962009-06-30 16:28:54 -0700162 * @throws IOException if the stream is not open
Nick Pelly9439a7f2009-06-30 12:04:36 -0700163 */
164 private void ensureOpen() throws IOException {
Tao Liejun3998bf02009-07-02 19:29:09 +0800165 mParent.ensureOpen();
166 if (!mOpen) {
Nick Pelly9439a7f2009-06-30 12:04:36 -0700167 throw new IOException("Input stream is closed");
168 }
169 }
170
171 /**
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700172 * Closes the input stream. If the input stream is already closed, do
Nick Pelly9439a7f2009-06-30 12:04:36 -0700173 * nothing.
Nick Pelly2e0da962009-06-30 16:28:54 -0700174 * @throws IOException this will never happen
Nick Pelly9439a7f2009-06-30 12:04:36 -0700175 */
176 @Override
177 public void close() throws IOException {
Tao Liejun3998bf02009-07-02 19:29:09 +0800178 mOpen = false;
179 mParent.streamClosed(true);
Nick Pelly9439a7f2009-06-30 12:04:36 -0700180 }
181}