blob: e78a4b3cdf658a320b93a168dd4662dd330ad69d [file] [log] [blame]
Robert Berry39194c02018-01-11 13:50:56 +00001/*
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
17package com.android.server.testing.shadows;
18
19import android.app.backup.BackupDataOutput;
20
21import org.robolectric.annotation.Implementation;
22import org.robolectric.annotation.Implements;
23
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000024import java.io.ByteArrayOutputStream;
Robert Berry39194c02018-01-11 13:50:56 +000025import java.io.FileDescriptor;
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000026import java.io.FileOutputStream;
Robert Berry39194c02018-01-11 13:50:56 +000027import java.io.IOException;
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000028import java.io.ObjectOutputStream;
Robert Berry39194c02018-01-11 13:50:56 +000029
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000030/**
31 * Shadow for {@link BackupDataOutput}. Format written does NOT match implementation. To read data
32 * written with this shadow you should also declare shadow {@link ShadowBackupDataInput}.
33 */
Robert Berry39194c02018-01-11 13:50:56 +000034@Implements(BackupDataOutput.class)
35public class ShadowBackupDataOutput {
36 private long mQuota;
37 private int mTransportFlags;
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000038 private ObjectOutputStream mOutput;
Robert Berry39194c02018-01-11 13:50:56 +000039
40 @Implementation
41 public void __constructor__(FileDescriptor fd, long quota, int transportFlags) {
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000042 try {
43 mOutput = new ObjectOutputStream(new FileOutputStream(fd));
44 } catch (IOException e) {
45 throw new AssertionError(e);
46 }
Robert Berry39194c02018-01-11 13:50:56 +000047 mQuota = quota;
48 mTransportFlags = transportFlags;
49 }
50
51 @Implementation
52 public long getQuota() {
53 return mQuota;
54 }
55
56 @Implementation
57 public int getTransportFlags() {
58 return mTransportFlags;
59 }
60
61 @Implementation
62 public int writeEntityHeader(String key, int dataSize) throws IOException {
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000063 final int size;
64 try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
65 writeEntityHeader(new ObjectOutputStream(byteStream), key, dataSize);
66 size = byteStream.size();
67 }
68 writeEntityHeader(mOutput, key, dataSize);
69 return size;
70 }
71
72 private void writeEntityHeader(ObjectOutputStream stream, String key, int dataSize)
73 throws IOException {
74 // Write the int first because readInt() throws EOFException, to know when stream ends
75 stream.writeInt(dataSize);
76 stream.writeUTF(key);
77 stream.flush();
Robert Berry39194c02018-01-11 13:50:56 +000078 }
79
80 @Implementation
81 public int writeEntityData(byte[] data, int size) throws IOException {
Bernardo Rufinob97ea7e2018-02-08 15:26:15 +000082 mOutput.write(data, 0, size);
83 mOutput.flush();
84 return size;
Robert Berry39194c02018-01-11 13:50:56 +000085 }
86}