summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputStream.java
blob: 72b8db01f0227a47219b92445124cb3934fdbd70 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.mozglue.JNIObject;

/**
 * This class provides an {@link InputStream} wrapper for a Gecko nsIChannel (or really,
 * nsIRequest).
 */
@WrapForJNI
@AnyThread
/* package */ class GeckoInputStream extends InputStream {
  private static final String LOGTAG = "GeckoInputStream";

  private LinkedList<ByteBuffer> mBuffers = new LinkedList<>();
  private boolean mEOF;
  private boolean mClosed;
  private boolean mHaveError;
  private long mReadTimeout;
  private boolean mResumed;
  private Support mSupport;

  /**
   * This is only called via JNI. The support instance provides callbacks for the native
   * counterpart.
   *
   * @param support An instance of {@link Support}, used for native callbacks.
   */
  /* package */ GeckoInputStream(final @Nullable Support support) {
    mSupport = support;
  }

  public void setReadTimeoutMillis(final long millis) {
    mReadTimeout = millis;
  }

  @Override
  public synchronized void close() throws IOException {
    super.close();
    mClosed = true;

    if (mSupport != null) {
      mSupport.close();
      mSupport = null;
    }
  }

  @Override
  public synchronized int available() throws IOException {
    if (mClosed) {
      return 0;
    }

    final ByteBuffer buf = mBuffers.peekFirst();
    return buf != null ? buf.remaining() : 0;
  }

  private void ensureNotClosed() throws IOException {
    if (mClosed) {
      throw new IOException("Stream is closed");
    }
  }

  @Override
  public synchronized int read() throws IOException {
    ensureNotClosed();

    final int expect = Integer.SIZE / 8;
    final byte[] bytes = new byte[expect];

    int count = 0;
    while (count < expect) {
      final long bytesRead = read(bytes, count, expect - count);
      if (bytesRead < 0) {
        return -1;
      }

      count += bytesRead;
    }

    final ByteBuffer buffer = ByteBuffer.wrap(bytes);
    return buffer.getInt();
  }

  @Override
  public int read(final @NonNull byte[] b) throws IOException {
    return read(b, 0, b.length);
  }

  @Override
  public synchronized int read(final @NonNull byte[] dest, final int offset, final int length)
      throws IOException {
    ensureNotClosed();

    final long startTime = System.currentTimeMillis();
    while (!mEOF && mBuffers.size() == 0) {
      if (mReadTimeout > 0 && (System.currentTimeMillis() - startTime) >= mReadTimeout) {
        throw new IOException("Timed out");
      }

      // The underlying channel is suspended, so resume that before
      // waiting for a buffer.
      if (!mResumed) {
        if (mSupport != null) {
          mSupport.resume();
        }
        mResumed = true;
      }

      try {
        wait(mReadTimeout);
      } catch (final InterruptedException e) {
      }
    }

    if (mEOF && mBuffers.size() == 0) {
      if (mHaveError) {
        throw new IOException("Unknown error");
      }

      // We have no data and we're not expecting more.
      return -1;
    }

    final ByteBuffer buf = mBuffers.peekFirst();
    final int readCount = Math.min(length, buf.remaining());
    buf.get(dest, offset, readCount);

    if (buf.remaining() == 0) {
      // We're done with this buffer, advance the queue.
      mBuffers.removeFirst();
    }

    return readCount;
  }

  /** Called by native code to indicate that no more data will be sent via {@link #appendBuffer}. */
  @WrapForJNI(calledFrom = "gecko")
  public synchronized void sendEof() {
    if (mEOF) {
      throw new IllegalStateException("Already have EOF");
    }

    mEOF = true;
    notifyAll();
  }

  /** Called by native code to indicate that there was an error while reading the stream. */
  @WrapForJNI(calledFrom = "gecko")
  public synchronized void sendError() {
    if (mEOF) {
      throw new IllegalStateException("Already have EOF");
    }

    mEOF = true;
    mHaveError = true;
    notifyAll();
  }

  /**
   * Called by native code to indicate that there was an issue during appending data to the stream.
   * The writing stream should still report EoF. Setting this error during writing will cause an
   * IOException if readers try to read from the stream.
   */
  @WrapForJNI(calledFrom = "gecko")
  public synchronized void writeError() {
    mHaveError = true;
    notifyAll();
  }

  /**
   * Called by native code to check if the stream is open.
   *
   * @return true if the stream is closed
   */
  @WrapForJNI(calledFrom = "gecko")
  /* package */ synchronized boolean isStreamClosed() {
    return mClosed || mEOF;
  }

  /**
   * Called by native code to provide data for this stream.
   *
   * @param buf the bytes
   * @throws IOException
   */
  @WrapForJNI(exceptionMode = "nsresult", calledFrom = "gecko")
  /* package */ synchronized void appendBuffer(final byte[] buf) throws IOException {

    if (mClosed) {
      throw new IllegalStateException("Stream is closed");
    }

    if (mEOF) {
      throw new IllegalStateException("EOF, no more data expected");
    }

    mBuffers.add(ByteBuffer.wrap(buf));
    notifyAll();
  }

  @WrapForJNI
  private static class Support extends JNIObject {
    @WrapForJNI(dispatchTo = "gecko")
    private native void resume();

    @WrapForJNI(dispatchTo = "gecko")
    private native void close();

    @Override // JNIObject
    protected void disposeNative() {
      throw new UnsupportedOperationException();
    }
  }
}