0001: /*
0002: * $Id: AnyFile.java,v 1.41 2002/09/16 08:05:02 jkl Exp $
0003: *
0004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
0005: *
0006: * Use is subject to license terms, as defined in
0007: * Anvil Sofware License, Version 1.1. See LICENSE
0008: * file, or http://njet.org/license-1.1.txt
0009: */
0010: package anvil.core.io;
0011:
0012: import anvil.core.Any;
0013: import anvil.core.AnyAbstractClass;
0014: import anvil.core.AnyBinary;
0015: import anvil.core.AnyString;
0016: import anvil.core.AnyList;
0017: import anvil.core.Array;
0018: import anvil.core.Serialization;
0019: import anvil.core.UnserializationException;
0020: import anvil.core.Unserializer;
0021: import anvil.core.Serializer;
0022: import anvil.script.Context;
0023: import anvil.java.util.BindingEnumeration;
0024: import anvil.java.io.GenericInputStream;
0025: import java.io.BufferedOutputStream;
0026: import java.io.File;
0027: import java.io.FileDescriptor;
0028: import java.io.FileInputStream;
0029: import java.io.FileOutputStream;
0030: import java.io.FilePermission;
0031: import java.io.IOException;
0032: import java.io.OutputStream;
0033: import java.io.RandomAccessFile;
0034: import java.io.Writer;
0035: import java.net.MalformedURLException;
0036: import java.util.Enumeration;
0037:
0038: /// @class File
0039: /// A representation of file and directory pathnames.
0040: ///
0041: /// @operator enumeration
0042: /// If this file points directory, return enumeration
0043: /// of files in directory, otherwise returns enumeration
0044: /// of lines in file.
0045: /// @synopsis enumeration * InstanceOfFile
0046:
0047: /**
0048: * class AnyFile
0049: *
0050: * @author: Jani Lehtimäki
0051: */
0052: public class AnyFile extends AnyAbstractClass {
0053:
0054: /// @constructor File
0055: /// Creates new File object.
0056: /// @synopsis File(string file)
0057: /// @synopsis File(string parent, string child)
0058: /// @synopsis File(File parent, string child)
0059: /// @param file Path to file
0060: /// @param parent Base path to merged with child
0061: /// @param child Child path to merged with parent
0062: public static final Object[] newInstance = { "context", "*child",
0063: null };
0064:
0065: public static final Any newInstance(Any p1, Any p2) {
0066: File file;
0067: if (p2 != null) {
0068: if (p1 instanceof AnyFile) {
0069: file = new File((File) p1.toObject(), IOModule
0070: .toPathname(p2));
0071: } else {
0072: file = new File(IOModule.toPathname(p1), IOModule
0073: .toPathname(p2));
0074: }
0075: } else {
0076: file = new File(IOModule.toPathname(p1));
0077: }
0078: return Any.create(file);
0079: }
0080:
0081: protected File _file;
0082: protected RandomAccessFile _access = null;
0083:
0084: public static final Any create(String file) {
0085: return new AnyFile(new File(file));
0086: }
0087:
0088: public AnyFile(File file) {
0089: _file = file;
0090: }
0091:
0092: public anvil.script.ClassType classOf() {
0093: return __class__;
0094: }
0095:
0096: public String toString() {
0097: return _file.toString();
0098: }
0099:
0100: public Writer toAnvil(Writer writer) throws IOException {
0101: writer.write("new anvil.io.File(\"");
0102: writer.write(anvil.util.Conversions.escape(_file.toString(),
0103: true));
0104: writer.write(')');
0105: writer.write('"');
0106: return writer;
0107: }
0108:
0109: public Writer toJava(Writer writer) throws IOException {
0110: writer.write("anvil.core.io.AnyFile.create(\"");
0111: writer.write(anvil.util.Conversions.escape(_file.toString(),
0112: true));
0113: writer.write('"');
0114: writer.write(')');
0115: return writer;
0116: }
0117:
0118: public anvil.codec.Code toCode(anvil.codec.Code code) {
0119: anvil.codec.ConstantPool pool = code.getPool();
0120: code.astring(_file.toString());
0121: code.invokestatic(code.getPool().addMethodRef(
0122: "anvil/core/io/AnyFile", "create",
0123: "(Ljava/lang/String;)Lanvil/core/Any;"));
0124: return code;
0125: }
0126:
0127: public Object toObject() {
0128: return _file;
0129: }
0130:
0131: public int hashCode() {
0132: return _file.hashCode();
0133: }
0134:
0135: public void serialize(Serializer serializer) throws IOException {
0136: if (serializer.register(this )) {
0137: return;
0138: }
0139: String s = _file.getAbsolutePath();
0140: serializer.write('F');
0141: serializer.write(s.length());
0142: serializer.write(':');
0143: serializer.writeUTF16(s);
0144: }
0145:
0146: public static final Any unserialize(Unserializer unserializer)
0147: throws UnserializationException {
0148: AnyFile file = new AnyFile(new File(unserializer
0149: .getUTF16String()));
0150: unserializer.register(file);
0151: return file;
0152: }
0153:
0154: public BindingEnumeration enumeration() {
0155: try {
0156: Context.getInstance().checkRead(_file.getPath());
0157: if (_file.isDirectory()) {
0158: File[] list = _file.listFiles();
0159: if (list == null) {
0160: return BindingEnumeration.EMPTY;
0161: }
0162: int n = list.length;
0163: Array files = new Array(n);
0164: for (int i = 0; i < n; i++) {
0165: files.append(Any.create(list[i]));
0166: }
0167: return files.keysAndElements();
0168:
0169: } else {
0170: return new InputStreamEnumeration(
0171: new GenericInputStream(new FileInputStream(
0172: _file)));
0173:
0174: }
0175:
0176: } catch (IOException e) {
0177: return BindingEnumeration.EMPTY;
0178: }
0179: }
0180:
0181: public boolean equals(Object o) {
0182: if (this == o) {
0183: return true;
0184: }
0185: if (o instanceof AnyFile) {
0186: return _file.equals(((AnyFile) o)._file);
0187: }
0188: return false;
0189: }
0190:
0191: /********* Exposed methods ************/
0192:
0193: /// @method getName
0194: /// Returns the name of file pointed by this File instance.
0195: /// @synopsis string getName()
0196: public Any m_getName() {
0197: return new AnyString(_file.getName());
0198: }
0199:
0200: /// @method getParent
0201: /// Returns the path of parent file pointed by this File instance.
0202: /// @synopsis string getParent()
0203: public Any m_getParent() {
0204: return new AnyString(_file.getParent());
0205: }
0206:
0207: /// @method getParentFile
0208: /// Returns the parent file pointed by this File instance.
0209: /// @synopsis File getParentFile()
0210: public Any m_getParentFile() {
0211: return Any.create(_file.getParentFile());
0212: }
0213:
0214: /// @method getPath
0215: /// Returns the path of file pointed by this File instance.
0216: /// @synopsis string getPath()
0217: public Any m_getPath() {
0218: return new AnyString(_file.getPath());
0219: }
0220:
0221: /// @method isAbsolute
0222: /// Tests if the file represented by this File object is an absolute pathname.
0223: /// @synopsis boolean isAbsolute()
0224: public Any m_isAbsolute() {
0225: return _file.isAbsolute() ? TRUE : FALSE;
0226: }
0227:
0228: /// @method getAbsolutePath
0229: /// Returns the absolute path.
0230: /// @synopsis string getAbsolutePath()
0231: public Any m_getAbsolutePath() {
0232: return new AnyString(_file.getAbsolutePath());
0233: }
0234:
0235: /// @method getAbsoluteFile
0236: /// Returns the absolute path as file.
0237: /// @synopsis File getAbsoluteFile()
0238: public Any m_getAbsoluteFile() {
0239: return Any.create(_file.getAbsoluteFile());
0240: }
0241:
0242: /// @method getCanonicalPath
0243: /// Returns the canonical path.
0244: /// @synopsis string getCanonicalPath()
0245: public Any m_getCanonicalPath(Context context) {
0246: try {
0247: return new AnyString(_file.getCanonicalPath());
0248: } catch (IOException e) {
0249: throw context.exception(e);
0250: }
0251: }
0252:
0253: /// @method getCanonicalFile
0254: /// Returns the canonical path as File.
0255: /// @synopsis File getCanonicalFile()
0256: public Any m_getCanonicalFile(Context context) {
0257: try {
0258: return Any.create(_file.getCanonicalFile());
0259: } catch (IOException e) {
0260: throw context.exception(e);
0261: }
0262: }
0263:
0264: /// @method toURL
0265: /// Converts path on this File variable to file:// URL.
0266: /// @synopsis URL toURL()
0267: public Any m_toURL(Context context) {
0268: try {
0269: return Any.create(_file.toURL());
0270: } catch (java.net.MalformedURLException e) {
0271: throw context.exception(e);
0272: }
0273: }
0274:
0275: /// @method canRead
0276: /// Checks if the contents of file can be read.
0277: /// @synopsis boolean canRead()
0278: public Any m_canRead(Context context) {
0279: context.checkRead(_file.getPath());
0280: return _file.canRead() ? TRUE : FALSE;
0281: }
0282:
0283: /// @method canWrite
0284: /// Checks if the contents of file can be written.
0285: /// @synopsis boolean canWrite()
0286: public Any m_canWrite(Context context) {
0287: context.checkWrite(_file.getPath());
0288: return _file.canWrite() ? TRUE : FALSE;
0289: }
0290:
0291: /// @method exists
0292: /// Checks if the file actually exists.
0293: /// @synopsis boolean exists()
0294: public Any m_exists(Context context) {
0295: context.checkRead(_file.getPath());
0296: return _file.exists() ? TRUE : FALSE;
0297: }
0298:
0299: /// @method isDirectory
0300: /// Checks if the file is directory.
0301: /// @synopsis boolean isDirectory()
0302: public Any m_isDirectory(Context context) {
0303: context.checkRead(_file.getPath());
0304: return _file.isDirectory() ? TRUE : FALSE;
0305: }
0306:
0307: /// @method isFile
0308: /// Checks if the file is regular file.
0309: /// @synopsis boolean isFile()
0310: public Any m_isFile(Context context) {
0311: context.checkRead(_file.getPath());
0312: return _file.isFile() ? TRUE : FALSE;
0313: }
0314:
0315: /// @method isHidden
0316: /// Checks is the file is hidden.
0317: /// @synopsis boolean isHidden()
0318: public Any m_isHidden(Context context) {
0319: context.checkRead(_file.getPath());
0320: return _file.isHidden() ? TRUE : FALSE;
0321: }
0322:
0323: /// @method lastModified
0324: /// Gets the timestamp of last modification (milliseconds
0325: /// since 1.1.1970).
0326: /// @synopsis int lastModified()
0327: public Any m_lastModified(Context context) {
0328: context.checkRead(_file.getPath());
0329: return Any.create(_file.lastModified());
0330: }
0331:
0332: /// @method createNewFile
0333: /// Creates the a new, empty file if and only if the file with this
0334: /// name does not yeat exist.
0335: /// @synopsis boolean createNewFile()
0336: /// @return <code>true</code> if the named file does not exist
0337: /// and was successfully created; <code>false</code> if the named
0338: /// file already exists
0339: /// @throws IOError If an I/O error occurred
0340: public Any m_createNewFile(Context context) {
0341: context.checkWrite(_file.getPath());
0342: try {
0343: return _file.createNewFile() ? TRUE : FALSE;
0344: } catch (IOException e) {
0345: throw context.exception(e);
0346: }
0347: }
0348:
0349: /// @method remove
0350: /// Removes the file. If this pathname denotes a directory, then the
0351: /// directory must be empty in order to be deleted.
0352: /// @return <code>true</code> if and only if the file or directory is
0353: /// successfully remove; false otherwise
0354: /// @synopsis boolean remove()
0355: public Any m_remove(Context context) {
0356: context.checkDelete(_file.getPath());
0357: return _file.delete() ? TRUE : FALSE;
0358: }
0359:
0360: /// @method removeOnExit
0361: /// Requests that the file or directory denoted by this abstract
0362: /// pathname be deleted when the virtual machine terminates.
0363: /// @synopsis File removeOnExit()
0364: public Any m_removeOnExit(Context context) {
0365: context.checkDelete(_file.getPath());
0366: _file.deleteOnExit();
0367: return this ;
0368: }
0369:
0370: /// @method list
0371: /// Returns an array of strings naming the files and directories
0372: /// in the directory denoted by this pathname.
0373: /// @synopsis array list()
0374: /// @return array of strings
0375: public Any m_list(Context context) {
0376: context.checkRead(_file.getPath());
0377: String[] list = _file.list();
0378: if (list == null) {
0379: return NULL;
0380: }
0381: int n = list.length;
0382: Array files = new Array(n);
0383: for (int i = 0; i < n; i++) {
0384: Any name = Any.create(list[i]);
0385: files.put(name, name);
0386: }
0387: return files;
0388: }
0389:
0390: /// @method listFiles
0391: /// Returns an array of File objects describing the files and directories
0392: /// in the directory denoted by this pathname.
0393: /// @synopsis array listFiles()
0394: /// @return array of Files containing files and directories, or
0395: /// <code>null</code> if this File does not point to directory.
0396: public Any m_listFiles(Context context) {
0397: context.checkRead(_file.getPath());
0398: File[] list = _file.listFiles();
0399: if (list == null) {
0400: return NULL;
0401: }
0402: int n = list.length;
0403: Array files = new Array(n);
0404: for (int i = 0; i < n; i++) {
0405: File file = list[i];
0406: files.put(Any.create(file.getName()), new AnyFile(file));
0407: }
0408: return files;
0409: }
0410:
0411: /// @method mkdir
0412: /// Creates the last part of path.
0413: /// @synopsis boolean mkdir()
0414: /// @return <code>true</code> if and only if the directory was created;
0415: /// <code>false</code> otherwise
0416: public Any m_mkdir(Context context) {
0417: context.checkWrite(_file.getPath());
0418: return _file.mkdir() ? TRUE : FALSE;
0419: }
0420:
0421: /// @method mkdirs
0422: /// Creates the full path.
0423: /// @synopsis boolean mkdirs()
0424: /// @return <code>true</code> if and only if all directories was created;
0425: /// <code>false</code> otherwise
0426: public Any m_mkdirs(Context context) {
0427: context.checkWrite(_file.getPath());
0428: return _file.mkdirs() ? TRUE : FALSE;
0429: }
0430:
0431: /// @method renameTo
0432: /// Renames the file.
0433: /// @synopsis boolean renameTo(string newName)
0434: /// @synopsis boolean renameTo(File newName)
0435: /// @return <code>true</code> if and only if the renaming succeeded;
0436: /// <code>false</code> otherwise
0437: public static final Object[] p_renameTo = new Object[] { null,
0438: "newName" };
0439:
0440: public Any m_renameTo(Context context, Any dest) {
0441: context.checkWrite(_file.getPath());
0442: File file = null;
0443: if (dest instanceof AnyFile) {
0444: file = ((AnyFile) dest)._file;
0445: } else {
0446: file = new File(dest.toString());
0447: }
0448: context.checkWrite(file.getPath());
0449: return _file.renameTo(file) ? TRUE : FALSE;
0450: }
0451:
0452: /// @method setLastModified
0453: /// Sets last modification timestamp.
0454: /// @synopsis boolean setLastModified(int timestamp)
0455: public static final Object[] p_setLastModified = new Object[] {
0456: null, "timestamp" };
0457:
0458: public Any m_setLastModified(Context context, long timestamp) {
0459: context.checkWrite(_file.getPath());
0460: return _file.setLastModified(timestamp) ? TRUE : FALSE;
0461: }
0462:
0463: /// @method setReadOnly
0464: /// Sets the file readonly.
0465: /// @synopsis boolean setReadOnly()
0466: public Any m_setReadOnly(Context context) {
0467: context.checkWrite(_file.getPath());
0468: return _file.setReadOnly() ? TRUE : FALSE;
0469: }
0470:
0471: /// @method openInput
0472: /// Opens an input stream to file.
0473: /// @synopsis InputStream openInput()
0474: /// @throws IOError If an I/O error occurs
0475: public Any m_openInput(Context context) {
0476: context.checkRead(_file.getPath());
0477: try {
0478: return new AnyInputStream(new FileInputStream(_file));
0479: } catch (IOException e) {
0480: throw context.exception(e);
0481: }
0482: }
0483:
0484: /// @method openOutput
0485: /// Opens an output stream to file.
0486: /// @synopsis OutputStream openOutput()
0487: /// @throws IOError If an I/O error occurs
0488: public Any m_openOutput(Context context) {
0489: context.checkWrite(_file.getPath());
0490: try {
0491: return new AnyOutputStream(new BufferedOutputStream(
0492: new FileOutputStream(_file)));
0493: } catch (IOException e) {
0494: throw context.exception(e);
0495: }
0496: }
0497:
0498: private void ensureOpen(Context context) throws IOException {
0499: ensureOpen(context, "r");
0500: }
0501:
0502: private void ensureOpen(Context context, String mode)
0503: throws IOException {
0504: if (mode.indexOf('r') >= 0) {
0505: context.checkRead(_file.getPath());
0506: }
0507: if (mode.indexOf('w') >= 0) {
0508: context.checkWrite(_file.getPath());
0509: }
0510: if (_access == null) {
0511: _access = new RandomAccessFile(_file, mode);
0512: }
0513: }
0514:
0515: /// @method open
0516: /// Opens file for reading (mode is <code>"r"</code>) or
0517: /// writing (mode is <code>"w"</code>). If the mode is
0518: /// <code>"rw"</code> and it does not exists, attempt is made
0519: /// to create it.
0520: /// @synopsis File open()
0521: /// @synopsis File open(string mode)
0522: /// @throws IOError If an I/O error occurs
0523: public static final Object[] p_open = new Object[] { null, "*mode",
0524: null };
0525:
0526: public Any m_open(Context context, String mode) {
0527: if (mode == null) {
0528: mode = "r";
0529: }
0530: try {
0531: ensureOpen(context, mode);
0532: return this ;
0533: } catch (IOException e) {
0534: throw context.exception(e);
0535: }
0536: }
0537:
0538: /// @method close
0539: /// Closes this random access file stream and releases any system resources associated
0540: /// with the stream. A closed file cannot perform input or output operations.
0541: /// @synopsis File close()
0542: /// @throws IOError If an I/O error occurs
0543: public Any m_close(Context context) {
0544: if (_access == null) {
0545: return this ;
0546: }
0547: try {
0548: _access.close();
0549: _access = null;
0550: return this ;
0551: } catch (IOException e) {
0552: throw context.exception(e);
0553: }
0554: }
0555:
0556: /// @method sync
0557: /// Attempts to synchronize the changes back to disk.
0558: /// @synopsis File sync()
0559: /// @throws IOError If an I/O error occurs
0560: public Any m_sync(Context context) {
0561: if (_access == null) {
0562: return this ;
0563: }
0564: try {
0565: _access.getFD().sync();
0566: return this ;
0567: } catch (IOException e) {
0568: throw context.exception(e);
0569: }
0570: }
0571:
0572: /// @method length
0573: /// Return the length of file.
0574: /// @synopsis int length()
0575: /// @throws IOError If an I/O error occurs
0576: public Any m_length(Context context) {
0577: try {
0578: if (_access == null) {
0579: context.checkRead(_file.getPath());
0580: return Any.create(_file.length());
0581: } else {
0582: return Any.create(_access.length());
0583: }
0584: } catch (IOException e) {
0585: throw context.exception(e);
0586: }
0587: }
0588:
0589: /// @method setLength
0590: /// Sets the length of file to 'newLength'. If 'newLength' is greater
0591: /// than the current length, contents of extended section is undefined.
0592: /// @synopsis int setLength(int newLength)
0593: /// @throws IOError If an I/O error occurs
0594: public static final Object[] p_setLength = new Object[] { null,
0595: "newLength" };
0596:
0597: public Any m_setLength(Context context, long newLength) {
0598: if (newLength < 0) {
0599: newLength = 0;
0600: }
0601: try {
0602: ensureOpen(context, "rw");
0603: _access.setLength(newLength);
0604: return TRUE;
0605: } catch (IOException e) {
0606: throw context.exception(e);
0607: }
0608: }
0609:
0610: /// @method seek
0611: /// Seeks file pointer to new position.
0612: /// @synopsis File seek(int pos)
0613: /// @param pos the offset position, measured in bytes
0614: /// from the beginning of the file,
0615: /// @throws IOError If an I/O error occurs
0616: public static final Object[] p_seek = new Object[] { null,
0617: "position" };
0618:
0619: public Any m_seek(Context context, long pos) {
0620: if (pos < 0) {
0621: pos = 0;
0622: }
0623: try {
0624: ensureOpen(context);
0625: _access.seek(pos);
0626: return this ;
0627: } catch (IOException e) {
0628: throw context.exception(e);
0629: }
0630: }
0631:
0632: /// @method getPos
0633: /// Returns the current file position.
0634: /// @synopsis int getPos()
0635: /// @throws IOError If an I/O error occurs
0636: public Any m_getPos(Context context) {
0637: try {
0638: ensureOpen(context);
0639: return Any.create(_access.getFilePointer());
0640: } catch (IOException e) {
0641: throw context.exception(e);
0642: }
0643: }
0644:
0645: /// @method skip
0646: /// Skips given amount of bytes.
0647: /// @synopsis int skip(int amount)
0648: /// @param amount Amount of bytes to skip
0649: /// @return number of bytes skipped.
0650: /// @throws IOError If an I/O error occurs
0651: public static final Object[] p_skip = new Object[] { null, "count" };
0652:
0653: public Any m_skip(Context context, int amount) {
0654: if (amount < 0) {
0655: return ZERO;
0656: }
0657: try {
0658: ensureOpen(context);
0659: return Any.create(_access.skipBytes(amount));
0660: } catch (IOException e) {
0661: throw context.exception(e);
0662: }
0663: }
0664:
0665: /// @method read
0666: /// @synopsis int read() ;
0667: /// Reads next byte from file
0668: /// @synopsis string read(int amount) ;
0669: /// Reads given amount of bytes from file.
0670: /// @param amout Amount of bytes to read
0671: /// @return string, or int, or <code>null</code> if end of stream
0672: /// encountered (and no bytes could not be read before it).
0673: /// @throws IOError If an I/O error occurs
0674: public static final Object[] p_read = new Object[] { null,
0675: "*bytes", null };
0676:
0677: public Any m_read(Context context, Any bytes) {
0678: try {
0679: if (bytes != null) {
0680: int amount = bytes.toInt();
0681: if (amount <= 0) {
0682: return EMPTY_STRING;
0683: }
0684: StringBuffer buffer = new StringBuffer(amount);
0685: int b;
0686: ensureOpen(context);
0687: while (((b = _access.read()) != -1) && (amount-- > 0)) {
0688: buffer.append((char) b);
0689: }
0690: return (b == -1) && (buffer.length() == 0) ? NULL
0691: : new AnyString(buffer.toString());
0692:
0693: } else {
0694: ensureOpen(context);
0695: int b = _access.read();
0696: return (b == -1) ? NULL : Any.create(b);
0697:
0698: }
0699: } catch (IOException e) {
0700: throw context.exception(e);
0701: }
0702: }
0703:
0704: /// @method readLine
0705: /// Reads a single line from file.
0706: /// @synopsis string readLine()
0707: /// @return line in string, or <code>null</code> if an error occured
0708: /// or end of stream encountered (and no bytes could not be read
0709: /// before it).
0710: /// @throws IOError If an I/O error occurs
0711: public Any m_readLine(Context context) {
0712: try {
0713: ensureOpen(context);
0714: return Any.create(_access.readLine());
0715: } catch (IOException e) {
0716: throw context.exception(e);
0717: }
0718: }
0719:
0720: /// @method readLines
0721: /// @synopsis list readLines() ;
0722: /// Reads as many lines as possible from this file
0723: /// @synopsis list readLines(int maxLines) ;
0724: /// Reads upto given amount of lines from this file
0725: /// @param maxLines Maximum number of lines to read
0726: /// @return List of strings
0727: /// @throws IOError If an I/O error occurs
0728: public static final Object[] p_readLines = new Object[] { null,
0729: "*maxLines", null };
0730:
0731: public Any m_readLines(Context context, Any max_) {
0732: int max = 0;
0733: if (max_ != null) {
0734: max = max_.toInt();
0735: if (max < 0) {
0736: max = 0;
0737: }
0738: }
0739: try {
0740: ensureOpen(context);
0741: String line;
0742: if (max == 0) {
0743: AnyList lines = new AnyList(new Any[8], 0);
0744: while ((line = _access.readLine()) != null) {
0745: lines.append(Any.create(line));
0746: }
0747: return lines;
0748: } else {
0749: Any[] lines = new Any[max];
0750: int index = 0;
0751: while (max-- > 0) {
0752: line = _access.readLine();
0753: if (line == null) {
0754: break;
0755: }
0756: lines[index++] = Any.create(line);
0757: }
0758: return new AnyList(lines, index);
0759: }
0760: } catch (IOException e) {
0761: throw context.exception(e);
0762: }
0763: }
0764:
0765: /// @method readData
0766: /// Reads and returns next serialized data from file.
0767: /// @synopsis object readData()
0768: /// @throws CorruptedSerialization If serialized data is correupted
0769: /// @throws IOError If an I/O error occurs
0770: public Any m_readData(Context context) {
0771: try {
0772: ensureOpen(context);
0773: return Serialization.unserialize(context, _access);
0774: } catch (UnserializationException e) {
0775: throw context.CorruptedSerialization();
0776: } catch (IOException e) {
0777: throw context.exception(e);
0778: }
0779: }
0780:
0781: /// @method readBinary
0782: /// Reads from file to given binary.
0783: /// @synopsis int readBinary(binary bin)
0784: /// @synopsis int readBinary(binary bin, int offset)
0785: /// @synopsis int readBinary(binary bin, int offset, int length)
0786: /// @param bin The binary into which the data is read
0787: /// @param offset The start offset of the data
0788: /// @param length The maximum number of bytes to read
0789: /// @return the amount of bytes read, or -1 if there is no more
0790: /// data because the end of file has been reached.
0791: /// @throws IOError If an I/O error occurs
0792: public static final Object[] p_readBinary = new Object[] { null,
0793: "array", "*offset", null, "*length", null };
0794:
0795: public Any m_readBinary(Context context, Any binary, Any offset_,
0796: Any length_) {
0797: byte[] array = null;
0798: if (binary.isBinary()) {
0799: array = binary.toBinary();
0800: } else {
0801: throw context.BadParameter("First parameter it not binary");
0802: }
0803: try {
0804: ensureOpen(context);
0805: int size = array.length;
0806: int offset = 0;
0807: int length = size;
0808: if (offset_ != null) {
0809: offset = offset_.toInt();
0810: if (length_ != null) {
0811: length = length_.toInt();
0812: }
0813: if (offset < 0) {
0814: offset = 0;
0815: }
0816: if (offset >= size) {
0817: return ZERO;
0818: }
0819: if (offset + length > size) {
0820: length = size - offset;
0821: }
0822: }
0823: return Any.create(_access.read(array, offset, length));
0824:
0825: } catch (IOException e) {
0826: throw context.exception(e);
0827: }
0828: }
0829:
0830: /// @method write
0831: /// @synopsis File write(int byte) ; Writes single byte to file
0832: /// @synopsis File write(binary bin) ; Writes bytes to file
0833: /// @synopsis File write(string text) ; Writes text to file
0834: /// @throws IOError If an I/O error occurs
0835: public static final Object[] p_write = new Object[] { null,
0836: "values" };
0837:
0838: public Any m_write(Context context, Any[] parameters) {
0839: int n = parameters.length;
0840: try {
0841: ensureOpen(context, "rw");
0842: for (int i = 0; i < n; i++) {
0843: Any value = parameters[i];
0844: switch (value.typeOf()) {
0845: case IS_NULL:
0846: case IS_UNDEFINED:
0847: break;
0848: case IS_INT:
0849: _access.write(value.toInt());
0850: break;
0851: case IS_BINARY:
0852: n = value.sizeOf();
0853: if (n > 0) {
0854: _access.write(value.toBinary(), 0, n);
0855: }
0856: break;
0857: default:
0858: String buffer = value.toString();
0859: if (buffer.length() > 0) {
0860: _access.writeBytes(buffer);
0861: }
0862: }
0863: }
0864: return this ;
0865: } catch (IOException e) {
0866: throw context.exception(e);
0867: }
0868: }
0869:
0870: /// @method writeBinary
0871: /// Writes the contents of binary to file.
0872: /// @synopsis File writeBinary(binary bin)
0873: /// @synopsis File writeBinary(binary bin, int offset, int length)
0874: /// @param bin The binary from which the data is written
0875: /// @param offset The start offset of the data
0876: /// @param length The number of bytes to write
0877: /// @throws IOError If an I/O error occurs
0878: public static final Object[] p_writeBinary = new Object[] { null,
0879: "array", "*offset", null, "*length", null };
0880:
0881: public Any m_writeBinary(Context context, Any binary, Any offset_,
0882: Any length_) {
0883: byte[] array;
0884: if (binary.isBinary()) {
0885: array = binary.toBinary();
0886: } else {
0887: throw context.BadParameter("First parameter it not binary");
0888: }
0889: int size = array.length;
0890: int offset = 0;
0891: int length = size;
0892: if (offset_ != null) {
0893: offset = offset_.toInt();
0894: if (length_ != null) {
0895: length = length_.toInt();
0896: }
0897: if (offset < 0) {
0898: offset = 0;
0899: }
0900: if (offset >= size) {
0901: return TRUE;
0902: }
0903: if (offset + length > size) {
0904: length = size - offset;
0905: }
0906: }
0907: try {
0908: ensureOpen(context, "rw");
0909: _access.write(array, offset, length);
0910: return TRUE;
0911: } catch (IOException e) {
0912: throw context.exception(e);
0913: }
0914: }
0915:
0916: /// @method writeData
0917: /// Serializes parameters to file.
0918: /// @synopsis File writeData(object data, ...)
0919: /// @param data Data to serialize
0920: /// @throws IOError If an I/O error occurs
0921: public static final Object[] p_writeData = new Object[] { null,
0922: "values" };
0923:
0924: public Any m_writeData(Context context, Any[] parameters) {
0925: int n = parameters.length;
0926: try {
0927: ensureOpen(context, "rw");
0928: for (int i = 0; i < n; i++) {
0929: Serialization
0930: .serialize(context, parameters[i], _access);
0931: }
0932: return this ;
0933: } catch (IOException e) {
0934: throw context.exception(e);
0935: }
0936: }
0937:
0938: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
0939: "File",
0940: AnyFile.class,
0941: //DOC{{
0942: ""
0943: + " @class File\n"
0944: + " A representation of file and directory pathnames. \n"
0945: + "\n"
0946: + " @operator enumeration\n"
0947: + " If this file points directory, return enumeration\n"
0948: + " of files in directory, otherwise returns enumeration\n"
0949: + " of lines in file.\n"
0950: + " @synopsis enumeration * InstanceOfFile\n"
0951: + " @constructor File\n"
0952: + " Creates new File object.\n"
0953: + " @synopsis File(string file)\n"
0954: + " @synopsis File(string parent, string child)\n"
0955: + " @synopsis File(File parent, string child)\n"
0956: + " @param file Path to file\n"
0957: + " @param parent Base path to merged with child\n"
0958: + " @param child Child path to merged with parent\n"
0959: + " @method getName\n"
0960: + " Returns the name of file pointed by this File instance.\n"
0961: + " @synopsis string getName()\n"
0962: + " @method getParent\n"
0963: + " Returns the path of parent file pointed by this File instance.\n"
0964: + " @synopsis string getParent()\n"
0965: + " @method getParentFile\n"
0966: + " Returns the parent file pointed by this File instance.\n"
0967: + " @synopsis File getParentFile()\n"
0968: + " @method getPath\n"
0969: + " Returns the path of file pointed by this File instance.\n"
0970: + " @synopsis string getPath()\n"
0971: + " @method isAbsolute \n"
0972: + " Tests if the file represented by this File object is an absolute pathname.\n"
0973: + " @synopsis boolean isAbsolute()\n"
0974: + " @method getAbsolutePath\n"
0975: + " Returns the absolute path.\n"
0976: + " @synopsis string getAbsolutePath()\n"
0977: + " @method getAbsoluteFile\n"
0978: + " Returns the absolute path as file.\n"
0979: + " @synopsis File getAbsoluteFile()\n"
0980: + " @method getCanonicalPath\n"
0981: + " Returns the canonical path.\n"
0982: + " @synopsis string getCanonicalPath()\n"
0983: + " @method getCanonicalFile\n"
0984: + " Returns the canonical path as File.\n"
0985: + " @synopsis File getCanonicalFile()\n"
0986: + " @method toURL\n"
0987: + " Converts path on this File variable to file:// URL.\n"
0988: + " @synopsis URL toURL()\n"
0989: + " @method canRead\n"
0990: + " Checks if the contents of file can be read.\n"
0991: + " @synopsis boolean canRead()\n"
0992: + " @method canWrite \n"
0993: + " Checks if the contents of file can be written.\n"
0994: + " @synopsis boolean canWrite()\n"
0995: + " @method exists\n"
0996: + " Checks if the file actually exists.\n"
0997: + " @synopsis boolean exists()\n"
0998: + " @method isDirectory\n"
0999: + " Checks if the file is directory.\n"
1000: + " @synopsis boolean isDirectory()\n"
1001: + " @method isFile \n"
1002: + " Checks if the file is regular file.\n"
1003: + " @synopsis boolean isFile()\n"
1004: + " @method isHidden\n"
1005: + " Checks is the file is hidden.\n"
1006: + " @synopsis boolean isHidden()\n"
1007: + " @method lastModified\n"
1008: + " Gets the timestamp of last modification (milliseconds\n"
1009: + " since 1.1.1970).\n"
1010: + " @synopsis int lastModified()\n"
1011: + " @method createNewFile\n"
1012: + " Creates the a new, empty file if and only if the file with this\n"
1013: + " name does not yeat exist.\n"
1014: + " @synopsis boolean createNewFile()\n"
1015: + " @return <code>true</code> if the named file does not exist \n"
1016: + " and was successfully created; <code>false</code> if the named \n"
1017: + " file already exists\n"
1018: + " @throws IOError If an I/O error occurred\n"
1019: + " @method remove \n"
1020: + " Removes the file. If this pathname denotes a directory, then the\n"
1021: + " directory must be empty in order to be deleted.\n"
1022: + " @return <code>true</code> if and only if the file or directory is \n"
1023: + " successfully remove; false otherwise\n"
1024: + " @synopsis boolean remove()\n"
1025: + " @method removeOnExit \n"
1026: + " Requests that the file or directory denoted by this abstract \n"
1027: + " pathname be deleted when the virtual machine terminates.\n"
1028: + " @synopsis File removeOnExit()\n"
1029: + " @method list \n"
1030: + " Returns an array of strings naming the files and directories \n"
1031: + " in the directory denoted by this pathname. \n"
1032: + " @synopsis array list()\n"
1033: + " @return array of strings\n"
1034: + " @method listFiles\n"
1035: + " Returns an array of File objects describing the files and directories \n"
1036: + " in the directory denoted by this pathname. \n"
1037: + " @synopsis array listFiles()\n"
1038: + " @return array of Files containing files and directories, or \n"
1039: + " <code>null</code> if this File does not point to directory.\n"
1040: + " @method mkdir \n"
1041: + " Creates the last part of path.\n"
1042: + " @synopsis boolean mkdir()\n"
1043: + " @return <code>true</code> if and only if the directory was created; \n"
1044: + " <code>false</code> otherwise\n"
1045: + " @method mkdirs \n"
1046: + " Creates the full path.\n"
1047: + " @synopsis boolean mkdirs()\n"
1048: + " @return <code>true</code> if and only if all directories was created; \n"
1049: + " <code>false</code> otherwise\n"
1050: + " @method renameTo\n"
1051: + " Renames the file.\n"
1052: + " @synopsis boolean renameTo(string newName)\n"
1053: + " @synopsis boolean renameTo(File newName)\n"
1054: + " @return <code>true</code> if and only if the renaming succeeded; \n"
1055: + " <code>false</code> otherwise\n"
1056: + " @method setLastModified\n"
1057: + " Sets last modification timestamp.\n"
1058: + " @synopsis boolean setLastModified(int timestamp)\n"
1059: + " @method setReadOnly\n"
1060: + " Sets the file readonly.\n"
1061: + " @synopsis boolean setReadOnly()\n"
1062: + " @method openInput\n"
1063: + " Opens an input stream to file.\n"
1064: + " @synopsis InputStream openInput()\n"
1065: + " @throws IOError If an I/O error occurs\n"
1066: + " @method openOutput\n"
1067: + " Opens an output stream to file.\n"
1068: + " @synopsis OutputStream openOutput()\n"
1069: + " @throws IOError If an I/O error occurs\n"
1070: + " @method open\n"
1071: + " Opens file for reading (mode is <code>\"r\"</code>) or \n"
1072: + " writing (mode is <code>\"w\"</code>). If the mode is \n"
1073: + " <code>\"rw\"</code> and it does not exists, attempt is made\n"
1074: + " to create it.\n"
1075: + " @synopsis File open()\n"
1076: + " @synopsis File open(string mode)\n"
1077: + " @throws IOError If an I/O error occurs\n"
1078: + " @method close\n"
1079: + " Closes this random access file stream and releases any system resources associated\n"
1080: + " with the stream. A closed file cannot perform input or output operations.\n"
1081: + " @synopsis File close()\n"
1082: + " @throws IOError If an I/O error occurs\n"
1083: + " @method sync\n"
1084: + " Attempts to synchronize the changes back to disk.\n"
1085: + " @synopsis File sync()\n"
1086: + " @throws IOError If an I/O error occurs\n"
1087: + " @method length\n"
1088: + " Return the length of file.\n"
1089: + " @synopsis int length()\n"
1090: + " @throws IOError If an I/O error occurs\n"
1091: + " @method setLength\n"
1092: + " Sets the length of file to 'newLength'. If 'newLength' is greater\n"
1093: + " than the current length, contents of extended section is undefined.\n"
1094: + " @synopsis int setLength(int newLength)\n"
1095: + " @throws IOError If an I/O error occurs\n"
1096: + " @method seek\n"
1097: + " Seeks file pointer to new position.\n"
1098: + " @synopsis File seek(int pos)\n"
1099: + " @param pos the offset position, measured in bytes \n"
1100: + " from the beginning of the file,\n"
1101: + " @throws IOError If an I/O error occurs\n"
1102: + " @method getPos \n"
1103: + " Returns the current file position.\n"
1104: + " @synopsis int getPos()\n"
1105: + " @throws IOError If an I/O error occurs\n"
1106: + " @method skip\n"
1107: + " Skips given amount of bytes. \n"
1108: + " @synopsis int skip(int amount)\n"
1109: + " @param amount Amount of bytes to skip\n"
1110: + " @return number of bytes skipped.\n"
1111: + " @throws IOError If an I/O error occurs\n"
1112: + " @method read\n"
1113: + " @synopsis int read() ; \n"
1114: + " Reads next byte from file\n"
1115: + " @synopsis string read(int amount) ; \n"
1116: + " Reads given amount of bytes from file. \n"
1117: + " @param amout Amount of bytes to read\n"
1118: + " @return string, or int, or <code>null</code> if end of stream \n"
1119: + " encountered (and no bytes could not be read before it).\n"
1120: + " @throws IOError If an I/O error occurs\n"
1121: + " @method readLine \n"
1122: + " Reads a single line from file. \n"
1123: + " @synopsis string readLine()\n"
1124: + " @return line in string, or <code>null</code> if an error occured\n"
1125: + " or end of stream encountered (and no bytes could not be read\n"
1126: + " before it).\n"
1127: + " @throws IOError If an I/O error occurs\n"
1128: + " @method readLines \n"
1129: + " @synopsis list readLines() ; \n"
1130: + " Reads as many lines as possible from this file\n"
1131: + " @synopsis list readLines(int maxLines) ;\n"
1132: + " Reads upto given amount of lines from this file\n"
1133: + " @param maxLines Maximum number of lines to read\n"
1134: + " @return List of strings\n"
1135: + " @throws IOError If an I/O error occurs\n"
1136: + " @method readData \n"
1137: + " Reads and returns next serialized data from file.\n"
1138: + " @synopsis object readData()\n"
1139: + " @throws CorruptedSerialization If serialized data is correupted\n"
1140: + " @throws IOError If an I/O error occurs\n"
1141: + " @method readBinary \n"
1142: + " Reads from file to given binary. \n"
1143: + " @synopsis int readBinary(binary bin)\n"
1144: + " @synopsis int readBinary(binary bin, int offset)\n"
1145: + " @synopsis int readBinary(binary bin, int offset, int length)\n"
1146: + " @param bin The binary into which the data is read\n"
1147: + " @param offset The start offset of the data\n"
1148: + " @param length The maximum number of bytes to read\n"
1149: + " @return the amount of bytes read, or -1 if there is no more\n"
1150: + " data because the end of file has been reached.\n"
1151: + " @throws IOError If an I/O error occurs\n"
1152: + " @method write\n"
1153: + " @synopsis File write(int byte) ; Writes single byte to file\n"
1154: + " @synopsis File write(binary bin) ; Writes bytes to file\n"
1155: + " @synopsis File write(string text) ; Writes text to file\n"
1156: + " @throws IOError If an I/O error occurs\n"
1157: + " @method writeBinary \n"
1158: + " Writes the contents of binary to file.\n"
1159: + " @synopsis File writeBinary(binary bin)\n"
1160: + " @synopsis File writeBinary(binary bin, int offset, int length)\n"
1161: + " @param bin The binary from which the data is written\n"
1162: + " @param offset The start offset of the data\n"
1163: + " @param length The number of bytes to write\n"
1164: + " @throws IOError If an I/O error occurs\n"
1165: + " @method writeData\n"
1166: + " Serializes parameters to file.\n"
1167: + " @synopsis File writeData(object data, ...)\n"
1168: + " @param data Data to serialize\n"
1169: + " @throws IOError If an I/O error occurs\n"
1170: //}}DOC
1171: );
1172: static {
1173: IOModule.class.getName();
1174: }
1175:
1176: }
|