1 /* 2 * Copyright (C) 2016 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 17 package com.android.layoutlib.bridge.util; 18 19 import com.android.tools.layoutlib.annotations.NotNull; 20 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.FileNotFoundException; 24 import java.io.IOException; 25 import java.io.InputStream; 26 27 /** 28 * Simpler wrapper around FileInputStream. This is used when the input stream represent 29 * not a normal bitmap but a nine patch. 30 * This is useful when the InputStream is created in a method but used in another that needs 31 * to know whether this is 9-patch or not, such as BitmapFactory. 32 */ 33 public class NinePatchInputStream extends InputStream { 34 private final InputStream mDelegate; 35 private boolean mFakeMarkSupport = true; 36 NinePatchInputStream(File file)37 public NinePatchInputStream(File file) throws FileNotFoundException { 38 mDelegate = new FileInputStream(file); 39 } 40 NinePatchInputStream(@otNull InputStream stream)41 public NinePatchInputStream(@NotNull InputStream stream) { 42 mDelegate = stream; 43 } 44 45 @Override markSupported()46 public boolean markSupported() { 47 // this is needed so that BitmapFactory doesn't wrap this in a BufferedInputStream. 48 return mFakeMarkSupport || mDelegate.markSupported(); 49 } 50 disableFakeMarkSupport()51 public void disableFakeMarkSupport() { 52 // disable fake mark support so that in case codec actually try to use them 53 // we don't lie to them. 54 mFakeMarkSupport = false; 55 } 56 57 @Override read()58 public int read() throws IOException { 59 return mDelegate.read(); 60 } 61 62 @Override read(byte[] b)63 public int read(byte[] b) throws IOException { 64 return mDelegate.read(b); 65 } 66 67 @Override read(byte[] b, int off, int len)68 public int read(byte[] b, int off, int len) throws IOException { 69 return mDelegate.read(b, off, len); 70 } 71 72 @Override skip(long n)73 public long skip(long n) throws IOException { 74 return mDelegate.skip(n); 75 } 76 77 @Override available()78 public int available() throws IOException { 79 return mDelegate.available(); 80 } 81 82 @Override close()83 public void close() throws IOException { 84 mDelegate.close(); 85 } 86 87 @Override mark(int readlimit)88 public void mark(int readlimit) { 89 mDelegate.mark(readlimit); 90 } 91 92 @Override reset()93 public void reset() throws IOException { 94 mDelegate.reset(); 95 } 96 } 97