1 /* 2 * Copyright (C) 2018 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 */ 16 package com.android.signapk; 17 import java.io.OutputStream; 18 import java.io.IOException; 19 20 class CountingOutputStream extends OutputStream { 21 private final OutputStream mBase; 22 private long mWrittenBytes; 23 CountingOutputStream(OutputStream base)24 public CountingOutputStream(OutputStream base) { 25 mBase = base; 26 } 27 28 @Override close()29 public void close() throws IOException { 30 mBase.close(); 31 } 32 33 @Override flush()34 public void flush() throws IOException { 35 mBase.flush(); 36 } 37 38 @Override write(byte[] b)39 public void write(byte[] b) throws IOException { 40 mBase.write(b); 41 mWrittenBytes += b.length; 42 } 43 44 @Override write(byte[] b, int off, int len)45 public void write(byte[] b, int off, int len) throws IOException { 46 mBase.write(b, off, len); 47 mWrittenBytes += len; 48 } 49 50 @Override write(int b)51 public void write(int b) throws IOException { 52 mBase.write(b); 53 mWrittenBytes += 1; 54 } 55 getWrittenBytes()56 public long getWrittenBytes() { 57 return mWrittenBytes; 58 } 59 } 60