blob: 127eee8023918a464866151785d7fbf95da7b3ac [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
John Reck28263772011-10-11 17:13:42 -070024 // Maximum size, this needs to be small enough such that an entire row
25 // can fit in CursorWindow's 2MB limit
26 private static final int MAX_SIZE = 1700000;
John Reck9d2718e2011-10-05 17:10:17 -070027 private ByteArrayOutputStream mStream;
28
29 public SnapshotByteArrayOutputStream() {
30 mStream = new ByteArrayOutputStream(MAX_SIZE);
31 }
32
33 @Override
34 public synchronized void write(int oneByte) throws IOException {
35 checkError(1);
36 mStream.write(oneByte);
37 }
38
39 @Override
40 public void write(byte[] buffer, int offset, int count) throws IOException {
41 checkError(count);
42 mStream.write(buffer, offset, count);
43 }
44
45 private void checkError(int expandBy) throws IOException {
46 if ((size() + expandBy) > MAX_SIZE) {
47 throw new IOException("Exceeded max size!");
48 }
49 }
50
51 public int size() {
52 return mStream.size();
53 }
54
55 public byte[] toByteArray() {
56 return mStream.toByteArray();
57 }
58
59}