public FileChannelClientDispatcher(string directory, string clientName, string channelName, IFileChannelFormatter channelFormatter, Action<object> onReceiveReplyMessage)
        {
            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException(nameof(directory));
            }

            if (string.IsNullOrEmpty(clientName))
            {
                throw new ArgumentNullException(nameof(clientName));
            }

            if (string.IsNullOrEmpty(channelName))
            {
                throw new ArgumentNullException(nameof(channelName));
            }

            if (channelFormatter == null)
            {
                throw new ArgumentNullException(nameof(channelFormatter));
            }

            if (onReceiveReplyMessage == null)
            {
                throw new ArgumentNullException(nameof(onReceiveReplyMessage));
            }

            _directory = directory;
            _requestFile = $"{clientName}.{channelName}.Request";
            _replyFile = $"{clientName}.{channelName}.Reply";
            _clientChannel = new FileChannel(directory, _replyFile, channelFormatter, onReceiveReplyMessage);
        }
Beispiel #2
0
        /// <exception cref="System.Exception"/>
        public virtual void TestMlock()
        {
            Assume.AssumeTrue(NativeIO.IsAvailable());
            FilePath TestFile = new FilePath(new FilePath(Runtime.GetProperty("test.build.data"
                                                                              , "build/test/data")), "testMlockFile");
            int BufLen = 12289;

            byte[] buf    = new byte[BufLen];
            int    bufSum = 0;

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i]  = unchecked ((byte)(i % 60));
                bufSum += buf[i];
            }
            FileOutputStream fos = new FileOutputStream(TestFile);

            try
            {
                fos.Write(buf);
                fos.GetChannel().Force(true);
            }
            finally
            {
                fos.Close();
            }
            FileInputStream fis     = null;
            FileChannel     channel = null;

            try
            {
                // Map file into memory
                fis     = new FileInputStream(TestFile);
                channel = fis.GetChannel();
                long             fileSize = channel.Size();
                MappedByteBuffer mapbuf   = channel.Map(FileChannel.MapMode.ReadOnly, 0, fileSize);
                // mlock the buffer
                NativeIO.POSIX.Mlock(mapbuf, fileSize);
                // Read the buffer
                int sum = 0;
                for (int i_1 = 0; i_1 < fileSize; i_1++)
                {
                    sum += mapbuf.Get(i_1);
                }
                Assert.Equal("Expected sums to be equal", bufSum, sum);
                // munmap the buffer, which also implicitly unlocks it
                NativeIO.POSIX.Munmap(mapbuf);
            }
            finally
            {
                if (channel != null)
                {
                    channel.Close();
                }
                if (fis != null)
                {
                    fis.Close();
                }
            }
        }
Beispiel #3
0
        static long CopyFileStream(FileInputStream @is, FileOutputStream os)
        {
            FileChannel srcChannel  = null;
            FileChannel destChannel = null;
            long        length;

            try
            {
                srcChannel  = @is.Channel;
                destChannel = os.Channel;
                length      = srcChannel.TransferTo(0L, srcChannel.Size(), destChannel);
            }
            finally
            {
                if (srcChannel != null)
                {
                    srcChannel.Close();
                }
                if (destChannel != null)
                {
                    destChannel.Close();
                }
            }
            return(length);
        }
Beispiel #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private java.nio.channels.FileLock createLockedStoreLockFileIn(java.nio.file.Path databaseDir) throws java.io.IOException
        private FileLock CreateLockedStoreLockFileIn(Path databaseDir)
        {
            Path storeLockFile = Files.createFile(DatabaseLayout.of(databaseDir.toFile()).StoreLayout.storeLockFile().toPath());

            _channel = FileChannel.open(storeLockFile, READ, WRITE);
            return(_channel.@lock(0, long.MaxValue, true));
        }
        /// <summary>
        /// Return the length of bytes in the given file after subtracting
        /// the trailer of 0xFF (OP_INVALID)s.
        /// </summary>
        /// <remarks>
        /// Return the length of bytes in the given file after subtracting
        /// the trailer of 0xFF (OP_INVALID)s.
        /// This seeks to the end of the file and reads chunks backwards until
        /// it finds a non-0xFF byte.
        /// </remarks>
        /// <exception cref="System.IO.IOException">if the file cannot be read</exception>
        private static long GetNonTrailerLength(FilePath f)
        {
            int             chunkSizeToRead = 256 * 1024;
            FileInputStream fis             = new FileInputStream(f);

            try
            {
                byte[]      buf  = new byte[chunkSizeToRead];
                FileChannel fc   = fis.GetChannel();
                long        size = fc.Size();
                long        pos  = size - (size % chunkSizeToRead);
                while (pos >= 0)
                {
                    fc.Position(pos);
                    int readLen = (int)Math.Min(size - pos, chunkSizeToRead);
                    IOUtils.ReadFully(fis, buf, 0, readLen);
                    for (int i = readLen - 1; i >= 0; i--)
                    {
                        if (buf[i] != FSEditLogOpCodes.OpInvalid.GetOpCode())
                        {
                            return(pos + i + 1);
                        }
                    }
                    // + 1 since we count this byte!
                    pos -= chunkSizeToRead;
                }
                return(0);
            }
            finally
            {
                fis.Close();
            }
        }
Beispiel #6
0
 /// <summary>
 /// Write a ByteBuffer to a FileChannel at a given offset,
 /// handling short writes.
 /// </summary>
 /// <param name="fc">The FileChannel to write to</param>
 /// <param name="buf">The input buffer</param>
 /// <param name="offset">The offset in the file to start writing at</param>
 /// <exception cref="System.IO.IOException">On I/O error</exception>
 public static void WriteFully(FileChannel fc, ByteBuffer buf, long offset)
 {
     do
     {
         offset += fc.Write(buf, offset);
     }while (buf.Remaining() > 0);
 }
        private BlockReaderLocal(BlockReaderLocal.Builder builder)
        {
            this.replica    = builder.replica;
            this.dataIn     = replica.GetDataStream().GetChannel();
            this.dataPos    = builder.dataPos;
            this.checksumIn = replica.GetMetaStream().GetChannel();
            BlockMetadataHeader header = builder.replica.GetMetaHeader();

            this.checksum       = header.GetChecksum();
            this.verifyChecksum = builder.verifyChecksum && (this.checksum.GetChecksumType().
                                                             id != DataChecksum.ChecksumNull);
            this.filename           = builder.filename;
            this.block              = builder.block;
            this.bytesPerChecksum   = checksum.GetBytesPerChecksum();
            this.checksumSize       = checksum.GetChecksumSize();
            this.maxAllocatedChunks = (bytesPerChecksum == 0) ? 0 : ((builder.bufferSize + bytesPerChecksum
                                                                      - 1) / bytesPerChecksum);
            // Calculate the effective maximum readahead.
            // We can't do more readahead than there is space in the buffer.
            int maxReadaheadChunks = (bytesPerChecksum == 0) ? 0 : ((Math.Min(builder.bufferSize
                                                                              , builder.maxReadahead) + bytesPerChecksum - 1) / bytesPerChecksum);

            if (maxReadaheadChunks == 0)
            {
                this.zeroReadaheadRequested = true;
                maxReadaheadChunks          = 1;
            }
            else
            {
                this.zeroReadaheadRequested = false;
            }
            this.maxReadaheadLength = maxReadaheadChunks * bytesPerChecksum;
            this.storageType        = builder.storageType;
        }
Beispiel #8
0
        /// <summary>
        /// Close all the files.
        /// </summary>
        /// <exception cref="SecurityException">  if a security manager exists and if
        ///             the caller does not have <tt>LoggingPermission("control")</tt>. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public synchronized void close() throws SecurityException
        public override void Close()
        {
            lock (this)
            {
                base.Close();
                // Unlock any lock file.
                if (LockFileName == null)
                {
                    return;
                }
                try
                {
                    // Close the lock file channel (which also will free any locks)
                    LockFileChannel.Close();
                }
                catch (Exception)
                {
                    // Problems closing the stream.  Punt.
                }
                lock (Locks)
                {
                    Locks.Remove(LockFileName);
                }
                if (System.IO.Directory.Exists(LockFileName))
                {
                    System.IO.Directory.Delete(LockFileName, true);
                }
                else
                {
                    System.IO.File.Delete(LockFileName);
                }
                LockFileName    = null;
                LockFileChannel = null;
            }
        }
Beispiel #9
0
 /// <summary>Truncate a block file</summary>
 /// <exception cref="System.IO.IOException"/>
 private long TruncateBlockFile()
 {
     lock (fds)
     {
         foreach (ReplicaInfo b in FsDatasetTestUtil.GetReplicas(fds, bpid))
         {
             FilePath f  = b.GetBlockFile();
             FilePath mf = b.GetMetaFile();
             // Truncate a block file that has a corresponding metadata file
             if (f.Exists() && f.Length() != 0 && mf.Exists())
             {
                 FileOutputStream s       = null;
                 FileChannel      channel = null;
                 try
                 {
                     s       = new FileOutputStream(f);
                     channel = s.GetChannel();
                     channel.Truncate(0);
                     Log.Info("Truncated block file " + f.GetAbsolutePath());
                     return(b.GetBlockId());
                 }
                 finally
                 {
                     IOUtils.Cleanup(Log, channel, s);
                 }
             }
         }
     }
     return(0);
 }
        public static bool CopyFile(File sourceFile, File destFile)
        {
            if (!destFile.Exists())
            {
                destFile.CreateNewFile();
            }

            FileChannel source      = null;
            FileChannel destination = null;

            try
            {
                source      = new FileInputStream(sourceFile).Channel;
                destination = new FileOutputStream(destFile).Channel;
                destination.TransferFrom(source, 0, source.Size());
            }
            catch (Exception e)
            {
                //FileLog.e("tmessages", e);
                return(false);
            }
            finally
            {
                if (source != null)
                {
                    source.Close();
                }

                if (destination != null)
                {
                    destination.Close();
                }
            }
            return(false);
        }
        public FileChannelServerDispatcher(string directory, string channelName, IFileChannelFormatter channelFormatter, Action<object> onReceiveRequestMessage)
        {
            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException(nameof(directory));
            }

            if (string.IsNullOrEmpty(channelName))
            {
                throw new ArgumentNullException(nameof(channelName));
            }

            if (channelFormatter == null)
            {
                throw new ArgumentNullException(nameof(channelFormatter));
            }

            if (onReceiveRequestMessage == null)
            {
                throw new ArgumentNullException(nameof(onReceiveRequestMessage));
            }

            var requestFileMask = $"*.{channelName}.Request";

            _channelName = channelName;
            _serverChannel = new FileChannel(directory, requestFileMask, channelFormatter, onReceiveRequestMessage);
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void truncate(java.io.File path, long size) throws java.io.IOException
        public override void Truncate(File path, long size)
        {
            using (FileChannel channel = FileChannel.open(path(path)))
            {
                channel.truncate(size);
            }
        }
        public FileChannelClientDispatcher(string directory, string clientName, string channelName, IFileChannelFormatter channelFormatter, Action <object> onReceiveReplyMessage)
        {
            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException(nameof(directory));
            }

            if (string.IsNullOrEmpty(clientName))
            {
                throw new ArgumentNullException(nameof(clientName));
            }

            if (string.IsNullOrEmpty(channelName))
            {
                throw new ArgumentNullException(nameof(channelName));
            }

            if (channelFormatter == null)
            {
                throw new ArgumentNullException(nameof(channelFormatter));
            }

            if (onReceiveReplyMessage == null)
            {
                throw new ArgumentNullException(nameof(onReceiveReplyMessage));
            }

            _directory     = directory;
            _requestFile   = $"{clientName}.{channelName}.Request";
            _replyFile     = $"{clientName}.{channelName}.Reply";
            _clientChannel = new FileChannel(directory, _replyFile, channelFormatter, onReceiveReplyMessage);
        }
Beispiel #14
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long lockPositionWriteVectorAndRetryIfInterrupted(long filePageId, java.nio.channels.FileChannel channel, long fileOffset, ByteBuffer[] srcs, int attemptsLeft) throws java.io.IOException
        private long LockPositionWriteVectorAndRetryIfInterrupted(long filePageId, FileChannel channel, long fileOffset, ByteBuffer[] srcs, int attemptsLeft)
        {
            try
            {
                long toWrite      = _filePageSize * ( long )srcs.Length;
                long bytesWritten = 0;
                lock (PositionLock(channel))
                {
                    channel.position(fileOffset);
                    do
                    {
                        bytesWritten += channel.write(srcs);
                    } while (bytesWritten < toWrite);
                    return(bytesWritten);
                }
            }
            catch (ClosedChannelException e)
            {
                TryReopen(filePageId, e);

                if (attemptsLeft < 1)
                {
                    throw new IOException("IO failed due to interruption", e);
                }

                bool interrupted = Thread.interrupted();
                channel = UnwrappedChannel(filePageId);
                long bytesWritten = LockPositionWriteVectorAndRetryIfInterrupted(filePageId, channel, fileOffset, srcs, attemptsLeft - 1);
                if (interrupted)
                {
                    Thread.CurrentThread.Interrupt();
                }
                return(bytesWritten);
            }
        }
Beispiel #15
0
        public FileChannelServerDispatcher(string directory, string channelName, IFileChannelFormatter channelFormatter, Action <object> onReceiveRequestMessage)
        {
            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException(nameof(directory));
            }

            if (string.IsNullOrEmpty(channelName))
            {
                throw new ArgumentNullException(nameof(channelName));
            }

            if (channelFormatter == null)
            {
                throw new ArgumentNullException(nameof(channelFormatter));
            }

            if (onReceiveRequestMessage == null)
            {
                throw new ArgumentNullException(nameof(onReceiveRequestMessage));
            }

            var requestFileMask = $"*.{channelName}.Request";

            _channelName   = channelName;
            _serverChannel = new FileChannel(directory, requestFileMask, channelFormatter, onReceiveRequestMessage);
        }
Beispiel #16
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long readPositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length) throws Exception
        private long ReadPositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length)
        {
            long        fileOffset = PageIdToPosition(startFilePageId);
            FileChannel channel    = UnwrappedChannel(startFilePageId);

            ByteBuffer[] srcs      = ConvertToByteBuffers(bufferAddresses, arrayOffset, length);
            long         bytesRead = LockPositionReadVectorAndRetryIfInterrupted(startFilePageId, channel, fileOffset, srcs, MAX_INTERRUPTED_CHANNEL_REOPEN_ATTEMPTS);

            if (bytesRead == -1)
            {
                foreach (long address in bufferAddresses)
                {
                    UnsafeUtil.setMemory(address, _filePageSize, MuninnPageCache.ZERO_BYTE);
                }
                return(0);
            }
            else if (bytesRead < (( long )_filePageSize) * length)
            {
                int pagesRead = ( int )(bytesRead / _filePageSize);
                int bytesReadIntoLastReadPage = ( int )(bytesRead % _filePageSize);
                int pagesNeedingZeroing       = length - pagesRead;
                for (int i = 0; i < pagesNeedingZeroing; i++)
                {
                    long address     = bufferAddresses[arrayOffset + pagesRead + i];
                    long bytesToZero = _filePageSize;
                    if (i == 0)
                    {
                        address     += bytesReadIntoLastReadPage;
                        bytesToZero -= bytesReadIntoLastReadPage;
                    }
                    UnsafeUtil.setMemory(address, bytesToZero, MuninnPageCache.ZERO_BYTE);
                }
            }
            return(bytesRead);
        }
Beispiel #17
0
 /// <exception cref="System.IO.IOException"/>
 public override void Close()
 {
     if (fp == null)
     {
         throw new IOException("Trying to use aborted output stream");
     }
     try
     {
         // close should have been called after all pending transactions
         // have been flushed & synced.
         // if already closed, just skip
         if (doubleBuf != null)
         {
             doubleBuf.Close();
             doubleBuf = null;
         }
         // remove any preallocated padding bytes from the transaction log.
         if (fc != null && fc.IsOpen())
         {
             fc.Truncate(fc.Position());
             fc.Close();
             fc = null;
         }
         fp.Close();
         fp = null;
     }
     finally
     {
         IOUtils.Cleanup(Log, fc, fp);
         doubleBuf = null;
         fc        = null;
         fp        = null;
     }
     fp = null;
 }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public StoreFileChannel open(java.io.File fileName, OpenMode openMode) throws java.io.IOException
        public override StoreFileChannel Open(File fileName, OpenMode openMode)
        {
            // Returning only the channel is ok, because the channel, when close()d will close its parent File.
            FileChannel channel = (new RandomAccessFile(fileName, openMode.mode())).Channel;

            return(GetStoreFileChannel(channel));
        }
Beispiel #19
0
        public virtual void TestRecoveryMode()
        {
            // edits generated by nnHelper (MiniDFSCluster), should have all op codes
            // binary, XML, reparsed binary
            string           edits = nnHelper.GenerateEdits();
            FileOutputStream os    = new FileOutputStream(edits, true);
            // Corrupt the file by truncating the end
            FileChannel editsFile = os.GetChannel();

            editsFile.Truncate(editsFile.Size() - 5);
            string editsParsedXml = folder.NewFile("editsRecoveredParsed.xml").GetAbsolutePath
                                        ();
            string editsReparsed   = folder.NewFile("editsRecoveredReparsed").GetAbsolutePath();
            string editsParsedXml2 = folder.NewFile("editsRecoveredParsed2.xml").GetAbsolutePath
                                         ();

            // Can't read the corrupted file without recovery mode
            NUnit.Framework.Assert.AreEqual(-1, RunOev(edits, editsParsedXml, "xml", false));
            // parse to XML then back to binary
            NUnit.Framework.Assert.AreEqual(0, RunOev(edits, editsParsedXml, "xml", true));
            NUnit.Framework.Assert.AreEqual(0, RunOev(editsParsedXml, editsReparsed, "binary"
                                                      , false));
            NUnit.Framework.Assert.AreEqual(0, RunOev(editsReparsed, editsParsedXml2, "xml",
                                                      false));
            // judgment time
            NUnit.Framework.Assert.IsTrue("Test round trip", FileUtils.ContentEqualsIgnoreEOL
                                              (new FilePath(editsParsedXml), new FilePath(editsParsedXml2), "UTF-8"));
            os.Close();
        }
Beispiel #20
0
        Load(long length, FileInputStream blockIn, FileInputStream metaIn, string blockFileName
             )
        {
            Org.Apache.Hadoop.Hdfs.Server.Datanode.Fsdataset.Impl.MappableBlock mappableBlock
                = null;
            MappedByteBuffer mmap         = null;
            FileChannel      blockChannel = null;

            try
            {
                blockChannel = blockIn.GetChannel();
                if (blockChannel == null)
                {
                    throw new IOException("Block InputStream has no FileChannel.");
                }
                mmap = blockChannel.Map(FileChannel.MapMode.ReadOnly, 0, length);
                NativeIO.POSIX.GetCacheManipulator().Mlock(blockFileName, mmap, length);
                VerifyChecksum(length, metaIn, blockChannel, blockFileName);
                mappableBlock = new Org.Apache.Hadoop.Hdfs.Server.Datanode.Fsdataset.Impl.MappableBlock
                                    (mmap, length);
            }
            finally
            {
                IOUtils.CloseQuietly(blockChannel);
                if (mappableBlock == null)
                {
                    if (mmap != null)
                    {
                        NativeIO.POSIX.Munmap(mmap);
                    }
                }
            }
            // unmapping also unlocks
            return(mappableBlock);
        }
Beispiel #21
0
        /// <exception cref="System.IO.IOException"></exception>
        public static void CopyFile(FilePath sourceFile, FilePath destFile)
        {
            if (!destFile.Exists())
            {
                destFile.CreateNewFile();
            }
            FileChannel source      = null;
            FileChannel destination = null;

            try
            {
                source      = new FileInputStream(sourceFile).GetChannel();
                destination = new FileOutputStream(destFile).GetChannel();
                destination.TransferFrom(source, 0, source.Size());
            }
            finally
            {
                if (source != null)
                {
                    source.Close();
                }
                if (destination != null)
                {
                    destination.Close();
                }
            }
        }
Beispiel #22
0
        /// <summary>
        /// Transfers data from FileChannel using
        /// <see cref="FileChannel.TransferTo(long, long, WritableByteChannel)
        ///     "/>
        /// .
        /// Updates <code>waitForWritableTime</code> and <code>transferToTime</code>
        /// with the time spent blocked on the network and the time spent transferring
        /// data from disk to network respectively.
        /// Similar to readFully(), this waits till requested amount of
        /// data is transfered.
        /// </summary>
        /// <param name="fileCh">FileChannel to transfer data from.</param>
        /// <param name="position">position within the channel where the transfer begins</param>
        /// <param name="count">number of bytes to transfer.</param>
        /// <param name="waitForWritableTime">
        /// nanoseconds spent waiting for the socket
        /// to become writable
        /// </param>
        /// <param name="transferTime">nanoseconds spent transferring data</param>
        /// <exception cref="System.IO.EOFException">
        ///
        /// If end of input file is reached before requested number of
        /// bytes are transfered.
        /// </exception>
        /// <exception cref="SocketTimeoutException">
        ///
        /// If this channel blocks transfer longer than timeout for
        /// this stream.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// Includes any exception thrown by
        /// <see cref="FileChannel.TransferTo(long, long, WritableByteChannel)
        ///     "/>
        /// .
        /// </exception>
        public virtual void TransferToFully(FileChannel fileCh, long position, int count,
                                            LongWritable waitForWritableTime, LongWritable transferToTime)
        {
            long waitTime     = 0;
            long transferTime = 0;

            while (count > 0)
            {
                /*
                 * Ideally we should wait after transferTo returns 0. But because of
                 * a bug in JRE on Linux (http://bugs.sun.com/view_bug.do?bug_id=5103988),
                 * which throws an exception instead of returning 0, we wait for the
                 * channel to be writable before writing to it. If you ever see
                 * IOException with message "Resource temporarily unavailable"
                 * thrown here, please let us know.
                 *
                 * Once we move to JAVA SE 7, wait should be moved to correct place.
                 */
                long start = Runtime.NanoTime();
                WaitForWritable();
                long wait        = Runtime.NanoTime();
                int  nTransfered = (int)fileCh.TransferTo(position, count, GetChannel());
                if (nTransfered == 0)
                {
                    //check if end of file is reached.
                    if (position >= fileCh.Size())
                    {
                        throw new EOFException("EOF Reached. file size is " + fileCh.Size() + " and " + count
                                               + " more bytes left to be " + "transfered.");
                    }
                }
                else
                {
                    //otherwise assume the socket is full.
                    //waitForWritable(); // see comment above.
                    if (nTransfered < 0)
                    {
                        throw new IOException("Unexpected return of " + nTransfered + " from transferTo()"
                                              );
                    }
                    else
                    {
                        position += nTransfered;
                        count    -= nTransfered;
                    }
                }
                long transfer = Runtime.NanoTime();
                waitTime     += wait - start;
                transferTime += transfer - wait;
            }
            if (waitForWritableTime != null)
            {
                waitForWritableTime.Set(waitTime);
            }
            if (transferToTime != null)
            {
                transferToTime.Set(transferTime);
            }
        }
 public LogFileMonitor(FileInfo file)
 {
     this.fileChannel = new FileChannel(file);
     watch.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
     watch.Filter = file.Name;
     watch.Changed += new FileSystemEventHandler(OnChanged);
     watch.Created += new FileSystemEventHandler(OnChanged);
 }
Beispiel #24
0
 public IrEncoder(ByteBuffer buffer, IntermediateRepresentation ir)
 {
     Channel      = null;
     ResultBuffer = buffer;
     this.Buffer  = ByteBuffer.allocateDirect(CAPACITY);
     DirectBuffer = new DirectBuffer(this.Buffer);
     this.Ir      = ir;
 }
 /// <exception cref="System.IO.IOException"/>
 public virtual void Close()
 {
     if (ch != null)
     {
         ch.Close();
         ch = null;
     }
 }
	public IrEncoder(ByteBuffer buffer, IntermediateRepresentation ir)
	{
		Channel = null;
		ResultBuffer = buffer;
		this.Buffer = ByteBuffer.allocateDirect(CAPACITY);
		DirectBuffer = new DirectBuffer(this.Buffer);
		this.Ir = ir;
	}
	public IrEncoder(string fileName, IntermediateRepresentation ir)
	{
		Channel = (new FileOutputStream(fileName)).Channel;
		ResultBuffer = null;
		Buffer = ByteBuffer.allocateDirect(CAPACITY);
		DirectBuffer = new DirectBuffer(Buffer);
		this.Ir = ir;
	}
Beispiel #28
0
 public IrEncoder(string fileName, IntermediateRepresentation ir)
 {
     Channel      = (new FileOutputStream(fileName)).Channel;
     ResultBuffer = null;
     Buffer       = ByteBuffer.allocateDirect(CAPACITY);
     DirectBuffer = new DirectBuffer(Buffer);
     this.Ir      = ir;
 }
Beispiel #29
0
 /// <summary>Copy the current file content into the temporary file.</summary>
 /// <remarks>
 /// Copy the current file content into the temporary file.
 /// <p>
 /// This method saves the current file content by inserting it into the
 /// temporary file, so that the caller can safely append rather than replace
 /// the primary file.
 /// <p>
 /// This method does nothing if the current file does not exist, or exists
 /// but is empty.
 /// </remarks>
 /// <exception cref="System.IO.IOException">
 /// the temporary file could not be written, or a read error
 /// occurred while reading from the current file. The lock is
 /// released before throwing the underlying IO exception to the
 /// caller.
 /// </exception>
 /// <exception cref="Sharpen.RuntimeException">
 /// the temporary file could not be written. The lock is released
 /// before throwing the underlying exception to the caller.
 /// </exception>
 public virtual void CopyCurrentContent()
 {
     RequireLock();
     try
     {
         FileInputStream fis = new FileInputStream(@ref);
         try
         {
             if (fsync)
             {
                 FileChannel @in = fis.GetChannel();
                 long        pos = 0;
                 long        cnt = @in.Size();
                 while (0 < cnt)
                 {
                     long r = os.GetChannel().TransferFrom(@in, pos, cnt);
                     pos += r;
                     cnt -= r;
                 }
             }
             else
             {
                 byte[] buf = new byte[2048];
                 int    r;
                 while ((r = fis.Read(buf)) >= 0)
                 {
                     os.Write(buf, 0, r);
                 }
             }
         }
         finally
         {
             fis.Close();
         }
     }
     catch (FileNotFoundException)
     {
     }
     catch (IOException ioe)
     {
         // Don't worry about a file that doesn't exist yet, it
         // conceptually has no current content to copy.
         //
         Unlock();
         throw;
     }
     catch (RuntimeException ioe)
     {
         Unlock();
         throw;
     }
     catch (Error ioe)
     {
         Unlock();
         throw;
     }
 }
Beispiel #30
0
 public static int MakeFileChannel(FileStream fileInputStream)
 {
     for (int i = 0; i < ChannelTable.Length; i++) {
         if (ChannelTable [i] == null) {
             ChannelTable [i] = new FileChannel (fileInputStream);
             return i;
         }
     }
     throw new NotImplementedException ();
 }
        private MappedByteBuffer getModel(AssetManager assets, string modelFilename)
        {
            AssetFileDescriptor fileDescriptor = assets.OpenFd(modelFilename);
            FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
            FileChannel         fileChannel    = inputStream.Channel;
            long startOffset    = fileDescriptor.StartOffset;
            long declaredLength = fileDescriptor.DeclaredLength;

            return(fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength));
        }
Beispiel #32
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long writePositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length) throws Exception
        private long WritePositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length)
        {
            long fileOffset = PageIdToPosition(startFilePageId);

            IncreaseFileSizeTo(fileOffset + ((( long )_filePageSize) * length));
            FileChannel channel = UnwrappedChannel(startFilePageId);

            ByteBuffer[] srcs = ConvertToByteBuffers(bufferAddresses, arrayOffset, length);
            return(LockPositionWriteVectorAndRetryIfInterrupted(startFilePageId, channel, fileOffset, srcs, MAX_INTERRUPTED_CHANNEL_REOPEN_ATTEMPTS));
        }
Beispiel #33
0
        /*
         * Memory-map the model file in Assets.
         */
        public static MappedByteBuffer LoadModelFile(Context context, string modelFile)
        {
            AssetFileDescriptor fileDescriptor = context.Assets.OpenFd(modelFile);
            FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
            FileChannel         fileChannel    = inputStream.Channel;
            long startOffset    = fileDescriptor.StartOffset;
            long declaredLength = fileDescriptor.DeclaredLength;

            return(fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength));
        }
Beispiel #34
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void main(String[] args) throws java.io.IOException
        public static void Main(string[] args)
        {
            Path path = Paths.get(args[0]);

            using (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE), java.nio.channels.FileLock @lock = channel.@lock())
            {
                Console.WriteLine(LOCKED_OUTPUT);
                System.out.flush();
                Console.Read();
            }
        }
Beispiel #35
0
		public long TransferFrom (FileChannel src, long pos, long count)
		{
			if (buffer == null)
				buffer = new byte [8092];
			int nr = src.s.Read (buffer, 0, (int) Math.Min (buffer.Length, count));
			long curPos = s.Position;
			s.Position = pos;
			s.Write (buffer, 0, nr);
			s.Position = curPos;
			return nr;
		}
Beispiel #36
0
        /// <exception cref="System.IO.IOException"></exception>
        private NGit.Storage.File.ReflogWriter Log(string refName, byte[] rec)
        {
            FilePath log   = LogFor(refName);
            bool     write = forceWrite || (IsLogAllRefUpdates() && ShouldAutoCreateLog(refName)) ||
                             log.IsFile();

            if (!write)
            {
                return(this);
            }
            WriteConfig      wc = GetRepository().GetConfig().Get(WriteConfig.KEY);
            FileOutputStream @out;

            try
            {
                @out = new FileOutputStream(log, true);
            }
            catch (FileNotFoundException err)
            {
                FilePath dir = log.GetParentFile();
                if (dir.Exists())
                {
                    throw;
                }
                if (!dir.Mkdirs() && !dir.IsDirectory())
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().cannotCreateDirectory,
                                                               dir));
                }
                @out = new FileOutputStream(log, true);
            }
            try
            {
                if (wc.GetFSyncRefFiles())
                {
                    FileChannel fc  = @out.GetChannel();
                    ByteBuffer  buf = ByteBuffer.Wrap(rec);
                    while (0 < buf.Remaining())
                    {
                        fc.Write(buf);
                    }
                    fc.Force(true);
                }
                else
                {
                    @out.Write(rec);
                }
            }
            finally
            {
                @out.Close();
            }
            return(this);
        }
 /// <summary>
 ///     Reads as much as possible from the channel into the buffer.
 /// </summary>
 /// <param name="channel"> The channel. </param>
 /// <param name="buffer"> The buffer. </param>
 /// <param name="ptr"> The initial position in the channel. </param>
 /// <exception cref="IOException"> if an I/O error occurs. </exception>
 /// <exception cref="EOFException">
 ///     if the end of the file was reached and the buffer
 ///     could not be completely populated.
 /// </exception>
 public static void ReadFully(FileChannel channel, ByteBuffer buffer, long ptr)
 {
     while (buffer.remaining() > 0)
     {
         long read = channel.read(buffer, ptr);
         if (read == -1)
         {
             throw new EOFException();
         }
         ptr += read;
     }
 }
        static void exportKml(string filename, string outputFile)
        {
            Document kml = new Document();


            FileChannel ch = new FileChannel(filename);
            UAVObjectManager mgr = new UAVObjectManager();
            UAVObjectsInitialize.register(mgr);
            UavTalk.UavTalkProto tlk = new UavTalkProto(ch, mgr);
            mgr.getObject<GPSPositionSensor>().onUpdated += new EventHandler(GpsPositionReceived);
            mgr.getObject<PositionState>().onUpdated += new EventHandler(GpsPositionReceived);

            ch.open();
            while (ch.isRunning)
                Thread.Sleep(50);


            LineString ls = new LineString();

            ls.AltitudeMode = SharpKml.Dom.AltitudeMode.Absolute;
            ls.Extrude = true;
            ls.Coordinates = coords;

            Placemark pm = new Placemark();

            pm.Name = "Flight Path ";
            //pm.StyleUrl = new Uri("#yellowLineGreenPoly", UriKind.Relative);
            pm.Geometry = ls;

            kml.AddFeature(pm);
            
            Serializer serializer = new Serializer();
            serializer.Serialize(kml);

            StreamWriter sw = new StreamWriter(outputFile);
            sw.Write(serializer.Xml);
            sw.Close();
        }
        private void updateFileData(FileChannel item)
        {
            bool isNew = (item.Channel.ChannelID == null);

            string[] lines = item.ReadLastBlock();

            if (isNew && item.Channel.ChannelID != null)
            {
                if (ChangedLogChannel != null)
                {
                    ChangedLogChannel(item.Channel);
                }
            }

            if (lines != null)
            {
                foreach (string line in lines)
                {
                    // ((FileChannel)item).LogCannel
                    if (LogMessage.isValidMessage(line))
                    {
                        LogMessage message = new LogMessage(item.Channel, line);
                        if (ChangedLogMessage != null)
                        {
                            ChangedLogMessage(message);
                        }
                    }
                }
            }
        }
    /// <summary> This procedure is invoked to process the "open" Tcl command.
    /// See the user documentation for details on what it does.
    /// 
    /// </summary>
    /// <param name="interp">the current interpreter.
    /// </param>
    /// <param name="argv">command arguments.
    /// </param>

    public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
    {

      bool pipeline = false; /* True if opening pipeline chan */
      int prot = 438; /* Final rdwr permissions of file */
      int modeFlags = TclIO.RDONLY; /* Rdwr mode for the file.  See the
			* TclIO class for more info on the
			* valid modes */

      if ( ( argv.Length < 2 ) || ( argv.Length > 4 ) )
      {
        throw new TclNumArgsException( interp, 1, argv, "fileName ?access? ?permissions?" );
      }

      if ( argv.Length > 2 )
      {
        TclObject mode = argv[2];

        string modeStr = mode.ToString();
        int len = modeStr.Length;

        // This "r+1" hack is just to get a test case to pass
        if ( ( len == 0 ) || ( modeStr.StartsWith( "r+" ) && len >= 3 ) )
        {
          throw new TclException( interp, "illegal access mode \"" + modeStr + "\"" );
        }

        if ( len < 3 )
        {
          switch ( modeStr[0] )
          {

            case 'r':
              {
                if ( len == 1 )
                {
                  modeFlags = TclIO.RDONLY;
                  break;
                }
                else if ( modeStr[1] == '+' )
                {
                  modeFlags = TclIO.RDWR;
                  break;
                }
              }
              goto case 'w';

            case 'w':
              {

                FileInfo f = FileUtil.getNewFileObj( interp, argv[1].ToString() );
                bool tmpBool;
                if ( File.Exists( f.FullName ) )
                  tmpBool = true;
                else
                  tmpBool = Directory.Exists( f.FullName );
                if ( tmpBool )
                {
                  bool tmpBool2;
                  try
                  {
                    if ( File.Exists( f.FullName ) )
                    {
                      File.SetAttributes( f.FullName, FileAttributes.Normal );
                      File.Delete( f.FullName );
                      tmpBool2 = true;
                    }
                    else if ( Directory.Exists( f.FullName ) )
                    {
                      Directory.Delete( f.FullName );
                      tmpBool2 = true;
                    }
                    else
                    {
                      tmpBool2 = false;
                    }
                  }
                  // ATK added because .NET do not allow often to delete
                  // files used by another process
                  catch ( IOException e )
                  {
                    throw new TclException( interp, "cannot open file: " + argv[1].ToString() );
                  }
                  bool generatedAux = tmpBool2;
                }
                if ( len == 1 )
                {
                  modeFlags = ( TclIO.WRONLY | TclIO.CREAT );
                  break;
                }
                else if ( modeStr[1] == '+' )
                {
                  modeFlags = ( TclIO.RDWR | TclIO.CREAT );
                  break;
                }
              }
              goto case 'a';

            case 'a':
              {
                if ( len == 1 )
                {
                  modeFlags = ( TclIO.WRONLY | TclIO.APPEND );
                  break;
                }
                else if ( modeStr[1] == '+' )
                {
                  modeFlags = ( TclIO.RDWR | TclIO.CREAT | TclIO.APPEND );
                  break;
                }
              }
              goto default;

            default:
              {
                throw new TclException( interp, "illegal access mode \"" + modeStr + "\"" );
              }

          }
        }
        else
        {
          modeFlags = 0;
          bool gotRorWflag = false;
          int mlen = TclList.getLength( interp, mode );
          for ( int i = 0; i < mlen; i++ )
          {
            TclObject marg = TclList.index( interp, mode, i );

            if ( marg.ToString().Equals( "RDONLY" ) )
            {
              modeFlags |= TclIO.RDONLY;
              gotRorWflag = true;
            }
            else
            {

              if ( marg.ToString().Equals( "WRONLY" ) )
              {
                modeFlags |= TclIO.WRONLY;
                gotRorWflag = true;
              }
              else
              {

                if ( marg.ToString().Equals( "RDWR" ) )
                {
                  modeFlags |= TclIO.RDWR;
                  gotRorWflag = true;
                }
                else
                {

                  if ( marg.ToString().Equals( "APPEND" ) )
                  {
                    modeFlags |= TclIO.APPEND;
                  }
                  else
                  {

                    if ( marg.ToString().Equals( "CREAT" ) )
                    {
                      modeFlags |= TclIO.CREAT;
                    }
                    else
                    {

                      if ( marg.ToString().Equals( "EXCL" ) )
                      {
                        modeFlags |= TclIO.EXCL;
                      }
                      else
                      {

                        if ( marg.ToString().Equals( "TRUNC" ) )
                        {
                          modeFlags |= TclIO.TRUNC;
                        }
                        else
                        {

                          throw new TclException( interp, "invalid access mode \"" + marg.ToString() + "\": must be RDONLY, WRONLY, RDWR, APPEND, " + "CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC" );
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          if ( !gotRorWflag )
          {
            throw new TclException( interp, "access mode must include either RDONLY, WRONLY, or RDWR" );
          }
        }
      }

      if ( argv.Length == 4 )
      {
        prot = TclInteger.get( interp, argv[3] );
        throw new TclException( interp, "setting permissions not implemented yet" );
      }

      if ( ( argv[1].ToString().Length > 0 ) && ( argv[1].ToString()[0] == '|' ) )
      {
        pipeline = true;
        throw new TclException( interp, "pipes not implemented yet" );
      }

      /*
      * Open the file or create a process pipeline.
      */

      if ( !pipeline )
      {
        try
        {
          FileChannel file = new FileChannel();

          file.open( interp, argv[1].ToString(), modeFlags );
          TclIO.registerChannel( interp, file );
          interp.setResult( file.ChanName );
        }
        catch ( IOException e )
        {

          throw new TclException( interp, "cannot open file: " + argv[1].ToString() );
        }
      }
      else
      {
        /*
        * Pipeline code here...
        */
      }
      return TCL.CompletionCode.RETURN;
    }