1/*
2 * Copyright (C) 2017 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.layout.remote.util;
18
19import com.android.tools.layoutlib.annotations.NotNull;
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.rmi.RemoteException;
24import java.rmi.server.UnicastRemoteObject;
25
26public class RemoteInputStreamAdapter implements RemoteInputStream {
27
28    private InputStream mDelegate;
29
30    private RemoteInputStreamAdapter(@NotNull InputStream delegate) {
31        mDelegate = delegate;
32    }
33
34    public static RemoteInputStream create(@NotNull InputStream is) throws RemoteException {
35        return (RemoteInputStream) UnicastRemoteObject.exportObject(
36                new RemoteInputStreamAdapter(is), 0);
37    }
38
39    @Override
40    public int read() throws IOException {
41        return mDelegate.read();
42    }
43
44    @Override
45    public byte[] read(int off, int len) throws IOException, RemoteException {
46        byte[] buffer = new byte[len];
47        if (mDelegate.read(buffer, off, len) == -1) {
48            throw new EndOfStreamException();
49        }
50        return buffer;
51    }
52
53    @Override
54    public long skip(long n) throws IOException {
55        return mDelegate.skip(n);
56    }
57
58    @Override
59    public int available() throws IOException {
60        return mDelegate.available();
61    }
62
63    @Override
64    public void close() throws IOException {
65        mDelegate.close();
66    }
67
68    @Override
69    public void mark(int readlimit) {
70        mDelegate.mark(readlimit);
71    }
72
73    @Override
74    public void reset() throws IOException {
75        mDelegate.reset();
76    }
77
78    @Override
79    public boolean markSupported() {
80        return mDelegate.markSupported();
81    }
82}
83