aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes/MCData.h
blob: ef7acda3a844fb0b65a2b06ad1166d90279a03e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef MAILCORE_MCDATA_H

#define MAILCORE_MCDATA_H

#include <stdlib.h>

#include <MailCore/MCObject.h>
#include <MailCore/MCMessageConstants.h>

#ifdef __APPLE__
#import <CoreFoundation/CoreFoundation.h>
#endif

#ifdef __cplusplus

namespace mailcore {
    
    class String;

    typedef void (*BytesDeallocator)(char * bytes, unsigned int length);

    class MAILCORE_EXPORT Data : public Object {
    public:
        Data();
        Data(int capacity);
        Data(const char * bytes, unsigned int length);
        virtual ~Data();
        
        static Data * data();
        static Data * dataWithCapacity(int capacity);
        static Data * dataWithContentsOfFile(String * filename);
        static Data * dataWithBytes(const char * bytes, unsigned int length);
        
        virtual char * bytes();
        virtual unsigned int length();

        virtual void increaseCapacity(unsigned int length);
        
        virtual void appendData(Data * otherData);
        virtual void appendBytes(const char * bytes, unsigned int length);
        virtual void setBytes(const char * bytes, unsigned int length);
        virtual void setData(Data * otherData);
        
        // Helpers
        virtual String * stringWithDetectedCharset();
        virtual String * stringWithDetectedCharset(String * charset, bool isHTML);
        virtual String * stringWithCharset(const char * charset);
        virtual Data * decodedDataUsingEncoding(Encoding encoding);

        virtual String * base64String();

        virtual ErrorCode writeToFile(String * filename);

    public: // private
        virtual String * charsetWithFilteredHTML(bool filterHTML, String * hintCharset = NULL);

        /* Replace contents of Data instance with contents of a given bytes without extra copying.
         Memory ownership transferred from client code to the Data instance.
         Data destructor will call bytesDeallocator function for memory releasing.
         */
        void takeBytesOwnership(char * bytes, unsigned int length, BytesDeallocator bytesDeallocator);

#ifdef __APPLE__
        virtual CFDataRef destructiveNSData();
#endif
        
    public: // subclass behavior
        Data(Data * otherData);
        virtual String * description();
        virtual Object * copy();
        virtual bool isEqual(Object * otherObject);
        virtual unsigned int hash();
        virtual HashMap * serializable();
        virtual void importSerializable(HashMap * serializable);
        
    private:
        char * mBytes;
        unsigned int mLength;
        unsigned int mAllocated;
        bool mExternallyAllocatedMemory;
        BytesDeallocator mBytesDeallocator;
        void allocate(unsigned int length, bool force = false);
        void init();
        void reset();
        String * charsetWithFilteredHTMLWithoutHint(bool filterHTML);
        
    };

}

#endif

#endif