• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.fastdeploy;
18 
19 import java.io.DataInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25 
26 class PatchUtils {
27     public static final String SIGNATURE = "FASTDEPLOY";
28 
29     /**
30      * Reads a 64-bit signed integer in Little Endian format from the specified {@link
31      * DataInputStream}.
32      *
33      * @param in the stream to read from.
34      */
readLELong(InputStream in)35     static long readLELong(InputStream in) throws IOException {
36         byte[] buffer = new byte[Long.BYTES];
37         readFully(in, buffer);
38         ByteBuffer buf = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN);
39         return buf.getLong();
40     }
41 
readString(InputStream in)42     static String readString(InputStream in) throws IOException {
43         int size = (int) readLELong(in);
44         byte[] buffer = new byte[size];
45         readFully(in, buffer);
46         return new String(buffer);
47     }
48 
readFully(final InputStream in, final byte[] destination, final int startAt, final int numBytes)49     static void readFully(final InputStream in, final byte[] destination, final int startAt,
50             final int numBytes) throws IOException {
51         int numRead = 0;
52         while (numRead < numBytes) {
53             int readNow = in.read(destination, startAt + numRead, numBytes - numRead);
54             if (readNow == -1) {
55                 throw new IOException("truncated input stream");
56             }
57             numRead += readNow;
58         }
59     }
60 
readFully(final InputStream in, final byte[] destination)61     static void readFully(final InputStream in, final byte[] destination) throws IOException {
62         readFully(in, destination, 0, destination.length);
63     }
64 
pipe(final InputStream in, final OutputStream out, final byte[] buffer, long copyLength)65     static void pipe(final InputStream in, final OutputStream out, final byte[] buffer,
66             long copyLength) throws IOException {
67         while (copyLength > 0) {
68             int maxCopy = (int) Math.min(buffer.length, copyLength);
69             readFully(in, buffer, 0, maxCopy);
70             out.write(buffer, 0, maxCopy);
71             copyLength -= maxCopy;
72         }
73     }
74 }
75