19d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck/* 29d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * Copyright (C) 2011 The Android Open Source Project 39d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * 49d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * Licensed under the Apache License, Version 2.0 (the "License"); 59d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * you may not use this file except in compliance with the License. 69d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * You may obtain a copy of the License at 79d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * 89d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * http://www.apache.org/licenses/LICENSE-2.0 99d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * 109d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * Unless required by applicable law or agreed to in writing, software 119d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * distributed under the License is distributed on an "AS IS" BASIS, 129d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * See the License for the specific language governing permissions and 149d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck * limitations under the License. 159d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck */ 169d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reckpackage com.android.browser; 179d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 189d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reckimport java.io.ByteArrayOutputStream; 199d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reckimport java.io.IOException; 209d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reckimport java.io.OutputStream; 219d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 229d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reckpublic class SnapshotByteArrayOutputStream extends OutputStream { 239d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 24282637771aaffd93f29fd72bf5cd49dd82d24d0dJohn Reck // Maximum size, this needs to be small enough such that an entire row 25282637771aaffd93f29fd72bf5cd49dd82d24d0dJohn Reck // can fit in CursorWindow's 2MB limit 26282637771aaffd93f29fd72bf5cd49dd82d24d0dJohn Reck private static final int MAX_SIZE = 1700000; 279d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck private ByteArrayOutputStream mStream; 289d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 299d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck public SnapshotByteArrayOutputStream() { 309d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck mStream = new ByteArrayOutputStream(MAX_SIZE); 319d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 329d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 339d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck @Override 349d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck public synchronized void write(int oneByte) throws IOException { 359d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck checkError(1); 369d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck mStream.write(oneByte); 379d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 389d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 399d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck @Override 409d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck public void write(byte[] buffer, int offset, int count) throws IOException { 419d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck checkError(count); 429d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck mStream.write(buffer, offset, count); 439d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 449d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 459d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck private void checkError(int expandBy) throws IOException { 469d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck if ((size() + expandBy) > MAX_SIZE) { 479d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck throw new IOException("Exceeded max size!"); 489d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 499d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 509d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 519d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck public int size() { 529d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck return mStream.size(); 539d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 549d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 559d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck public byte[] toByteArray() { 569d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck return mStream.toByteArray(); 579d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck } 589d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck 599d2718e7a2d58ce87a18ca4987553fd996180f3cJohn Reck} 60