| 1 |
/* |
| 2 |
* Copyright (C) 2015 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.tools.fd.runtime; |
| 17 |
|
| 18 |
import static com.android.tools.fd.common.Log.logging; |
| 19 |
|
| 20 |
import com.android.annotations.NonNull; |
| 21 |
import com.android.annotations.Nullable; |
| 22 |
|
| 23 |
import java.io.DataInputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.List; |
| 27 |
import java.util.logging.Level; |
| 28 |
|
| 29 |
// This class is used in both the Android runtime and in the IDE. |
| 30 |
// Technically we only need the write protocol on the IDE side and the |
| 31 |
// read protocol on the Android app size, but keeping it all together and |
| 32 |
// in sync right now. |
| 33 |
public class ApplicationPatch { |
| 34 |
@NonNull public final String path; |
| 35 |
@NonNull public final byte[] data; |
| 36 |
|
| 37 |
public ApplicationPatch(@NonNull String path, @NonNull byte[] data) { |
| 38 |
this.path = path; |
| 39 |
this.data = data; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String toString() { |
| 44 |
return "ApplicationPatch{" + |
| 45 |
"path='" + path + '\'' + |
| 46 |
", data.length='" + data.length + '\'' + |
| 47 |
'}'; |
| 48 |
} |
| 49 |
|
| 50 |
// Only needed on the Android side |
| 51 |
@Nullable |
| 52 |
public static List<ApplicationPatch> read(@NonNull DataInputStream input) throws IOException { |
| 53 |
int changeCount = input.readInt(); |
| 54 |
|
| 55 |
if (logging != null && logging.isLoggable(Level.FINE)) { |
| 56 |
logging.log(Level.FINE, "Receiving " + changeCount + " changes"); |
| 57 |
} |
| 58 |
|
| 59 |
List<ApplicationPatch> changes = new ArrayList<ApplicationPatch>(changeCount); |
| 60 |
for (int i = 0; i < changeCount; i++) { |
| 61 |
String path = input.readUTF(); |
| 62 |
int size = input.readInt(); |
| 63 |
byte[] bytes = new byte[size]; |
| 64 |
input.readFully(bytes); |
| 65 |
changes.add(new ApplicationPatch(path, bytes)); |
| 66 |
} |
| 67 |
|
| 68 |
return changes; |
| 69 |
} |
| 70 |
|
| 71 |
@NonNull |
| 72 |
public String getPath() { |
| 73 |
return path; |
| 74 |
} |
| 75 |
|
| 76 |
@NonNull |
| 77 |
public byte[] getBytes() { |
| 78 |
return data; |
| 79 |
} |
| 80 |
} |