Exemplo n.º 1
0
        public virtual bool writeToFile(cape.File file)
        {
            if (!(file != null))
            {
                return(false);
            }
            if (getIsDirectory())
            {
                return(file.createDirectoryRecursive());
            }
            var reader = getContentReader();

            if (!(reader != null))
            {
                return(false);
            }
            var fp = file.getParent();

            if (fp != null)
            {
                fp.createDirectoryRecursive();
            }
            var writer = file.write();

            if (!(writer != null))
            {
                if (reader is cape.Closable)
                {
                    ((cape.Closable)reader).close();
                }
                return(false);
            }
            var buf = new byte[4096 * 4];
            var v   = true;
            var n   = 0;

            while ((n = reader.read(buf)) > 0)
            {
                var nr = writer.write(buf, n);
                if (nr != n)
                {
                    v = false;
                    break;
                }
            }
            if (!v)
            {
                file.remove();
            }
            if (reader != null && reader is cape.Closable)
            {
                ((cape.Closable)reader).close();
            }
            if (writer != null && writer is cape.Closable)
            {
                ((cape.Closable)writer).close();
            }
            return(v);
        }
Exemplo n.º 2
0
        public override bool move(cape.File dest, bool replace)
        {
            onError(null);
            if (!(dest != null))
            {
                onError("null destination");
                return(false);
            }
            if (dest.exists())
            {
                if (!replace)
                {
                    onError("target file already exists");
                    return(false);
                }
                if (dest.remove() == false)
                {
                    onError("Error when removing `" + dest.getPath() + "': " + dest.getLastErrorDescription());
                    return(false);
                }
            }
            var destf = dest as cape.FileForDotNet;

            if (!(destf != null))
            {
                return(false);
            }
            var v = true;

            try {
                System.IO.File.Move(completePath, destf.completePath);
            }
            catch (System.Exception e) {
                onError(e.ToString());
                v = false;
            }
            return(v);
        }
Exemplo n.º 3
0
        public virtual bool copyFileTo(cape.File dest)
        {
            if (dest == null)
            {
                return(false);
            }
            if (this.isSame(dest))
            {
                return(true);
            }
            var buf = new byte[4096 * 4];

            if (buf == null)
            {
                return(false);
            }
            var reader = this.read();

            if (reader == null)
            {
                return(false);
            }
            var writer = dest.write();

            if (writer == null)
            {
                if (reader is cape.Closable)
                {
                    ((cape.Closable)reader).close();
                }
                return(false);
            }
            var v = true;
            var n = 0;

            while ((n = reader.read(buf)) > 0)
            {
                var nr = writer.write(buf, n);
                if (nr != n)
                {
                    v = false;
                    break;
                }
            }
            if (v)
            {
                var fi = this.stat();
                if (fi != null)
                {
                    var mode = fi.getMode();
                    if (mode != 0)
                    {
                        dest.setMode(mode);
                    }
                }
            }
            else
            {
                dest.remove();
            }
            if (reader != null && reader is cape.Closable)
            {
                ((cape.Closable)reader).close();
            }
            if (writer != null && writer is cape.Closable)
            {
                ((cape.Closable)writer).close();
            }
            return(v);
        }