1 /* 2 * Copyright 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 17 package com.android.tools.build.jetifier.processor.archive 18 19 import java.io.IOException 20 import java.io.OutputStream 21 import java.nio.file.Path 22 23 /** 24 * Represents a file in the archive that is not an archive. 25 */ 26 class ArchiveFile(relativePath: Path, data: ByteArray) : ArchiveItem { 27 28 override var relativePath = relativePath 29 private set 30 31 override var fileName: String = relativePath.fileName.toString() 32 private set 33 34 override var wasChanged: Boolean = false 35 private set 36 37 var data: ByteArray = data 38 private set 39 40 override fun accept(visitor: ArchiveItemVisitor) { 41 visitor.visit(this) 42 } 43 44 @Throws(IOException::class) 45 override fun writeSelfTo(outputStream: OutputStream) { 46 outputStream.write(data) 47 } 48 49 fun updateRelativePath(newRelativePath: Path) { 50 if (relativePath != newRelativePath) { 51 wasChanged = true 52 } 53 54 relativePath = newRelativePath 55 fileName = relativePath.fileName.toString() 56 } 57 58 /** 59 * Sets new data while also marking this file as changed. This will result into the parent 60 * archive also being considered as changed thus marking it as dependent on the Support library. 61 */ 62 fun setNewData(newData: ByteArray) { 63 data = newData 64 wasChanged = true 65 } 66 67 /** 68 * Sets a potentially new data without triggering a change. Useful in cases the change is not 69 * significant for the refactoring because it occurred due to some optimization or 70 * formatting change. 71 * 72 * If there was at least one genuine change in any file of the parent archive this won't prevent 73 * this file from being updated. However this will prevent the change to propagate to 74 * the parent archive which would otherwise mark it as dependent on the Support Library. 75 */ 76 fun setNewDataSilently(newData: ByteArray) { 77 data = newData 78 } 79 }