summaryrefslogtreecommitdiffstats
path: root/obex/javax/obex/PrivateOutputStream.java
blob: 713f4ae2cff3cc7b5ea279965edaea6ccb640256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2008-2009, Motorola, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * - Neither the name of the Motorola, Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package javax.obex;

import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;

/**
 * This object provides an output stream to the Operation objects used in this
 * package.
 * @hide
 */
public final class PrivateOutputStream extends OutputStream {

    private BaseStream mParent;

    private ByteArrayOutputStream mArray;

    private boolean mOpen;

    private int mMaxPacketSize;

    /**
     * Creates an empty <code>PrivateOutputStream</code> to write to.
     * @param p the connection that this stream runs over
     */
    public PrivateOutputStream(BaseStream p, int maxSize) {
        mParent = p;
        mArray = new ByteArrayOutputStream();
        mMaxPacketSize = maxSize;
        mOpen = true;
    }

    /**
     * Determines how many bytes have been written to the output stream.
     * @return the number of bytes written to the output stream
     */
    public int size() {
        return mArray.size();
    }

    /**
     * Writes the specified byte to this output stream. The general contract for
     * write is that one byte is written to the output stream. The byte to be
     * written is the eight low-order bits of the argument b. The 24 high-order
     * bits of b are ignored.
     * @param b the byte to write
     * @throws IOException if an I/O error occurs
     */
    @Override
    public synchronized void write(int b) throws IOException {
        ensureOpen();
        mParent.ensureNotDone();
        mArray.write(b);
        if (mArray.size() == mMaxPacketSize) {
            mParent.continueOperation(true, false);
        }
    }

    @Override
    public void write(byte[] buffer) throws IOException {
        write(buffer, 0, buffer.length);
    }

    @Override
    public synchronized void write(byte[] buffer, int offset, int count) throws IOException {
        int offset1 = offset;
        int remainLength = count;

        if (buffer == null) {
            throw new IOException("buffer is null");
        }
        if ((offset | count) < 0 || count > buffer.length - offset) {
            throw new IndexOutOfBoundsException("index outof bound");
        }

        ensureOpen();
        mParent.ensureNotDone();
        while ((mArray.size() + remainLength) >= mMaxPacketSize) {
            int bufferLeft = mMaxPacketSize - mArray.size();
            mArray.write(buffer, offset1, bufferLeft);
            offset1 += bufferLeft;
            remainLength -= bufferLeft;
            mParent.continueOperation(true, false);
        }
        if (remainLength > 0) {
            mArray.write(buffer, offset1, remainLength);
        }
    }

    /**
     * Reads the bytes that have been written to this stream.
     * @param size the size of the array to return
     * @return the byte array that is written
     */
    public synchronized byte[] readBytes(int size) {
        if (mArray.size() > 0) {
            byte[] temp = mArray.toByteArray();
            mArray.reset();
            byte[] result = new byte[size];
            System.arraycopy(temp, 0, result, 0, size);
            if (temp.length != size) {
                mArray.write(temp, size, temp.length - size);
            }
            return result;
        } else {
            return null;
        }
    }

    /**
     * Verifies that this stream is open
     * @throws IOException if the stream is not open
     */
    private void ensureOpen() throws IOException {
        mParent.ensureOpen();
        if (!mOpen) {
            throw new IOException("Output stream is closed");
        }
    }

    /**
     * Closes the output stream. If the input stream is already closed, do
     * nothing.
     * @throws IOException this will never happen
     */
    @Override
    public void close() throws IOException {
        mOpen = false;
        mParent.streamClosed(false);
    }

    /**
     * Determines if the connection is closed
     * @return <code>true</code> if the connection is closed; <code>false</code>
     *         if the connection is open
     */
    public boolean isClosed() {
        return !mOpen;
    }
}