blob: bfd1a76a994836f326b2642261e2e516bcce5737 [file] [log] [blame]
John Reck9d2718e2011-10-05 17:10:17 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.browser;
17
18import java.io.ByteArrayOutputStream;
19import java.io.IOException;
20import java.io.OutputStream;
21
22public class SnapshotByteArrayOutputStream extends OutputStream {
23
24 // Maximum size, just below CursorWindow's 2mb row limit
25 private static final int MAX_SIZE = 2000000;
26 private ByteArrayOutputStream mStream;
27
28 public SnapshotByteArrayOutputStream() {
29 mStream = new ByteArrayOutputStream(MAX_SIZE);
30 }
31
32 @Override
33 public synchronized void write(int oneByte) throws IOException {
34 checkError(1);
35 mStream.write(oneByte);
36 }
37
38 @Override
39 public void write(byte[] buffer, int offset, int count) throws IOException {
40 checkError(count);
41 mStream.write(buffer, offset, count);
42 }
43
44 private void checkError(int expandBy) throws IOException {
45 if ((size() + expandBy) > MAX_SIZE) {
46 throw new IOException("Exceeded max size!");
47 }
48 }
49
50 public int size() {
51 return mStream.size();
52 }
53
54 public byte[] toByteArray() {
55 return mStream.toByteArray();
56 }
57
58}