1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
45 FileClass::FileClass(VTable
*cvtable
)
46 : ClassClosure(cvtable
)
48 createVanillaPrototype();
51 bool FileClass::exists(Stringp filename
)
54 toplevel()->throwArgumentError(kNullArgumentError
, "filename");
59 StUTF8String
filenameUTF8(filename
);
60 File
* fp
= Platform::GetInstance()->createFile();
63 if (fp
->open(filenameUTF8
.c_str(), File::OPEN_READ
)) {
67 Platform::GetInstance()->destroyFile(fp
);
72 Stringp
FileClass::read(Stringp filename
)
74 Toplevel
* toplevel
= this->toplevel();
75 AvmCore
* core
= this->core();
78 toplevel
->throwArgumentError(kNullArgumentError
, "filename");
80 StUTF8String
filenameUTF8(filename
);
81 File
* fp
= Platform::GetInstance()->createFile();
82 if(!fp
|| !fp
->open(filenameUTF8
.c_str(), File::OPEN_READ
))
86 Platform::GetInstance()->destroyFile(fp
);
89 toplevel
->throwError(kFileOpenError
, filename
);
92 int64_t fileSize
= fp
->size();
93 if(fileSize
>= (int64_t)INT32_T_MAX
) //File APIs cannot handle files > 2GB
95 toplevel
->throwRangeError(kOutOfRangeError
, filename
);
98 int len
= (int)fileSize
;
100 // Avoid VMPI_alloca - the buffer can be large and the memory is non-pointer-containing,
101 // but the GC will scan it conservatively.
102 uint8_t* c
= (uint8_t*)core
->gc
->Alloc(len
+1);
104 len
= (int)fp
->read(c
, len
); //need to force since the string creation functions expect an int
108 Platform::GetInstance()->destroyFile(fp
);
115 if ((c
[0] == 0xef) && (c
[1] == 0xbb) && (c
[2] == 0xbf))
117 ret
= core
->newStringUTF8((const char*)c
+ 3, len
- 3);
119 else if ((c
[0] == 0xfe) && (c
[1] == 0xff))
123 len
= (len
- 2) >> 1;
124 ret
= core
->newStringEndianUTF16(/*littleEndian*/false, (wchar
*)(void*)c
, len
);
126 else if ((c
[0] == 0xff) && (c
[1] == 0xfe))
128 //UTF-16 little endian
130 len
= (len
- 2) >> 1;
131 ret
= core
->newStringEndianUTF16(/*littleEndian*/true, (wchar
*)(void*)c
, len
);
137 // Use newStringUTF8() with "strict" explicitly set to false to mimick old,
138 // buggy behavior, where malformed UTF-8 sequences are stored as single characters.
139 ret
= core
->newStringUTF8((const char*)c
, len
, false);
146 void FileClass::write(Stringp filename
,
149 Toplevel
* toplevel
= this->toplevel();
152 toplevel
->throwArgumentError(kNullArgumentError
, "filename");
155 toplevel
->throwArgumentError(kNullArgumentError
, "data");
157 StUTF8String
filenameUTF8(filename
);
158 File
* fp
= Platform::GetInstance()->createFile();
159 if (!fp
|| !fp
->open(filenameUTF8
.c_str(), File::OPEN_WRITE
))
163 Platform::GetInstance()->destroyFile(fp
);
165 toplevel
->throwError(kFileWriteError
, filename
);
167 StUTF8String
dataUTF8(data
);
168 if ((int32_t)fp
->write(dataUTF8
.c_str(), dataUTF8
.length()) != dataUTF8
.length()) {
169 toplevel
->throwError(kFileWriteError
, filename
);
172 Platform::GetInstance()->destroyFile(fp
);
175 ByteArrayObject
* FileClass::readByteArray(Stringp filename
)
177 Toplevel
* toplevel
= this->toplevel();
179 toplevel
->throwArgumentError(kNullArgumentError
, "filename");
181 StUTF8String
filenameUTF8(filename
);
183 File
* fp
= Platform::GetInstance()->createFile();
184 if (fp
== NULL
|| !fp
->open(filenameUTF8
.c_str(), File::OPEN_READ_BINARY
))
188 Platform::GetInstance()->destroyFile(fp
);
190 toplevel
->throwError(kFileOpenError
, filename
);
193 int64_t len
= fp
->size();
194 if((uint64_t)len
>= UINT32_T_MAX
) //ByteArray APIs cannot handle files > 4GB
196 toplevel
->throwRangeError(kOutOfRangeError
, filename
);
199 uint32_t readCount
= (uint32_t)len
;
201 unsigned char *c
= mmfx_new_array( unsigned char, readCount
+1);
203 ByteArrayObject
* b
= toplevel
->byteArrayClass()->constructByteArray();
206 while (readCount
> 0)
208 uint32_t actual
= (uint32_t) fp
->read(c
, readCount
);
211 b
->GetByteArray().Write(c
, actual
);
212 readCount
-= readCount
;
221 mmfx_delete_array( c
);
224 Platform::GetInstance()->destroyFile(fp
);
229 bool FileClass::writeByteArray(Stringp filename
, ByteArrayObject
* bytes
)
231 Toplevel
* toplevel
= this->toplevel();
233 toplevel
->throwArgumentError(kNullArgumentError
, "filename");
236 StUTF8String
filenameUTF8(filename
);
238 File
* fp
= Platform::GetInstance()->createFile();
239 if (fp
== NULL
|| !fp
->open(filenameUTF8
.c_str(), File::OPEN_WRITE_BINARY
))
243 Platform::GetInstance()->destroyFile(fp
);
245 toplevel
->throwError(kFileWriteError
, filename
);
248 int32_t len
= bytes
->get_length();
249 bool success
= (int32_t)fp
->write(&(bytes
->GetByteArray())[0], len
) == len
;
252 Platform::GetInstance()->destroyFile(fp
);
255 toplevel
->throwError(kFileWriteError
, filename
);