Utils.h revision ca32021b43f326af7d3f4ae041f8db297f98a518
1/*
2 * Copyright (C) 2006 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#ifndef UTILS_DEFINED
18#define UTILS_DEFINED
19
20#include "SkStream.h"
21
22#include "android_util_Binder.h"
23
24#include <jni.h>
25#include <androidfw/Asset.h>
26
27namespace android {
28
29class AssetStreamAdaptor : public SkStreamRewindable {
30public:
31    AssetStreamAdaptor(Asset* a) : fAsset(a) {}
32    virtual bool rewind();
33    virtual size_t read(void* buffer, size_t size);
34    virtual bool hasLength() const { return true; }
35    virtual size_t getLength() const;
36    virtual bool isAtEnd() const;
37
38    virtual SkStreamRewindable* duplicate() const;
39private:
40    Asset*  fAsset;
41};
42
43/**
44 *  Make a deep copy of the asset, and return it as a stream, or NULL if there
45 *  was an error.
46 *  FIXME: If we could "ref/reopen" the asset, we may not need to copy it here.
47 */
48
49SkMemoryStream* CopyAssetToStream(Asset*);
50
51/** Restore the file descriptor's offset in our destructor
52 */
53class AutoFDSeek {
54public:
55    AutoFDSeek(int fd) : fFD(fd) {
56        fCurr = ::lseek(fd, 0, SEEK_CUR);
57    }
58    ~AutoFDSeek() {
59        if (fCurr >= 0) {
60            ::lseek(fFD, fCurr, SEEK_SET);
61        }
62    }
63private:
64    int     fFD;
65    off64_t   fCurr;
66};
67
68jobject nullObjectReturn(const char msg[]);
69
70}; // namespace android
71
72#endif
73