Exemple #1
0
 /// <summary>Write a ByteBuffer to a WritableByteChannel, handling short writes.</summary>
 /// <param name="bc">The WritableByteChannel to write to</param>
 /// <param name="buf">The input buffer</param>
 /// <exception cref="System.IO.IOException">On I/O error</exception>
 public static void WriteFully(WritableByteChannel bc, ByteBuffer buf)
 {
     do
     {
         bc.Write(buf);
     }while (buf.Remaining() > 0);
 }
Exemple #2
0
 private StreamToDisk(WritableByteChannel writableByteChannel, params AutoCloseable[] closeables)
 {
     this._writableByteChannel = writableByteChannel;
     this._closeables          = new List <AutoCloseable>();
     this._closeables.Add(writableByteChannel);
     ((IList <AutoCloseable>) this._closeables).AddRange(Arrays.asList(closeables));
 }
Exemple #3
0
 private static void CustomShuffleTransferCornerCases(FadvisedFileRegion fileRegion
                                                      , WritableByteChannel target, int count)
 {
     try
     {
         fileRegion.CustomShuffleTransfer(target, -1);
         NUnit.Framework.Assert.Fail("Expected a IllegalArgumentException");
     }
     catch (ArgumentException)
     {
         Log.Info("Expected - illegal argument is passed.");
     }
     catch (Exception)
     {
         NUnit.Framework.Assert.Fail("Expected a IllegalArgumentException");
     }
     //test corner cases
     try
     {
         fileRegion.CustomShuffleTransfer(target, count + 1);
         NUnit.Framework.Assert.Fail("Expected a IllegalArgumentException");
     }
     catch (ArgumentException)
     {
         Log.Info("Expected - illegal argument is passed.");
     }
     catch (Exception)
     {
         NUnit.Framework.Assert.Fail("Expected a IllegalArgumentException");
     }
 }
Exemple #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void write(java.io.OutputStream out) throws java.io.IOException
        public virtual void write(System.IO.Stream @out)
        {
            DataOutputStream data = new DataOutputStream(@out);

            data.writeInt(packetSize);
            data.writeInt(address);
            data.writeInt(Length);

            if (buffer is ByteBuffer)
            {
                WritableByteChannel channel = Channels.newChannel(@out);
                channel.write((ByteBuffer)buffer);
            }
            else
            {
                IMemoryReader reader = MemoryReader.getMemoryReader(address, Length, 1);
                for (int i = 0; i < Length; i++)
                {
                    data.writeByte(reader.readNext());
                }
            }

            VideoEngine.log_Renamed.info(string.Format("Saved memory {0:x8} - {1:x8} (len {2:x8})", address, address + Length, Length));
            //VideoEngine.Console.WriteLine("CaptureRAM write " + ((3 * 4) + Length));

            //data.flush();
            //out.flush();
        }
Exemple #5
0
        /// <summary>
        /// Write all remaining bytes in buffer to the given channel.
        /// If the channel is selectable then it must be configured blocking.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void writeFullyImpl(WritableByteChannel ch, java.nio.ByteBuffer bb) throws java.io.IOException
        private static void WriteFullyImpl(WritableByteChannel ch, ByteBuffer bb)
        {
            while (bb.Remaining() > 0)
            {
                int n = ch.Write(bb);
                if (n <= 0)
                {
                    throw new RuntimeException("no bytes written");
                }
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected void generateFileWithRecords(java.nio.channels.WritableByteChannel channel, int recordCount, int recordSize) throws java.io.IOException
        protected internal virtual void GenerateFileWithRecords(WritableByteChannel channel, int recordCount, int recordSize)
        {
            ByteBuffer buf = ByteBuffer.allocate(recordSize);

            for (int i = 0; i < recordCount; i++)
            {
                GenerateRecordForId(i, buf);
                int rem = buf.remaining();
                do
                {
                    rem -= channel.write(buf);
                } while (rem > 0);
            }
        }
 /// <exception cref="System.IO.IOException"/>
 public override long TransferTo(WritableByteChannel target, long position)
 {
     if (manageOsCache && readaheadPool != null)
     {
         readaheadRequest = readaheadPool.ReadaheadStream(identifier, fd, GetPosition() +
                                                          position, readaheadLength, GetPosition() + GetCount(), readaheadRequest);
     }
     if (this.shuffleTransferToAllowed)
     {
         return(base.TransferTo(target, position));
     }
     else
     {
         return(CustomShuffleTransfer(target, position));
     }
 }
        internal virtual long CustomShuffleTransfer(WritableByteChannel target, long position
                                                    )
        {
            long actualCount = this.count - position;

            if (actualCount < 0 || position < 0)
            {
                throw new ArgumentException("position out of range: " + position + " (expected: 0 - "
                                            + (this.count - 1) + ')');
            }
            if (actualCount == 0)
            {
                return(0L);
            }
            long       trans = actualCount;
            int        readSize;
            ByteBuffer byteBuffer = ByteBuffer.Allocate(this.shuffleBufferSize);

            while (trans > 0L && (readSize = fileChannel.Read(byteBuffer, this.position + position
                                                              )) > 0)
            {
                //adjust counters and buffer limit
                if (readSize < trans)
                {
                    trans    -= readSize;
                    position += readSize;
                    byteBuffer.Flip();
                }
                else
                {
                    //We can read more than we need if the actualCount is not multiple
                    //of the byteBuffer size and file is big enough. In that case we cannot
                    //use flip method but we need to set buffer limit manually to trans.
                    byteBuffer.Limit((int)trans);
                    byteBuffer.Position(0);
                    position += trans;
                    trans     = 0;
                }
                //write data to the target
                while (byteBuffer.HasRemaining())
                {
                    target.Write(byteBuffer);
                }
                byteBuffer.Clear();
            }
            return(actualCount - trans);
        }
Exemple #9
0
        /// <summary>
        /// Write all remaining bytes in buffer to the given channel.
        /// </summary>
        /// <exception cref="IllegalBlockingModeException">
        ///          If the channel is selectable and configured non-blocking. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static void writeFully(WritableByteChannel ch, java.nio.ByteBuffer bb) throws java.io.IOException
        private static void WriteFully(WritableByteChannel ch, ByteBuffer bb)
        {
            if (ch is SelectableChannel)
            {
                SelectableChannel sc = (SelectableChannel)ch;
                lock (sc.BlockingLock())
                {
                    if (!sc.Blocking)
                    {
                        throw new IllegalBlockingModeException();
                    }
                    WriteFullyImpl(ch, bb);
                }
            }
            else
            {
                WriteFullyImpl(ch, bb);
            }
        }
Exemple #10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void fastCopy(java.io.InputStream paramInputStream, java.io.OutputStream paramOutputStream) throws java.io.IOException
        public static void fastCopy(Stream paramInputStream, Stream paramOutputStream)
        {
            ReadableByteChannel readableByteChannel = Channels.newChannel(paramInputStream);
            WritableByteChannel writableByteChannel = Channels.newChannel(paramOutputStream);
            ByteBuffer          byteBuffer          = ByteBuffer.allocateDirect(16384);

            while (readableByteChannel.read(byteBuffer) != -1)
            {
                byteBuffer.flip();
                writableByteChannel.write(byteBuffer);
                byteBuffer.compact();
            }
            byteBuffer.flip();
            while (byteBuffer.hasRemaining())
            {
                writableByteChannel.write(byteBuffer);
            }
            readableByteChannel.close();
            writableByteChannel.close();
        }
 public virtual void Reset(WritableByteChannel channel)
 {
     this._channel = channel;
 }
Exemple #12
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestCustomShuffleTransfer()
        {
            FilePath absLogDir = new FilePath("target", typeof(TestFadvisedFileRegion).Name +
                                              "LocDir").GetAbsoluteFile();
            string testDirPath = StringUtils.Join(Path.Separator, new string[] { absLogDir.GetAbsolutePath
                                                                                     (), "testCustomShuffleTransfer" });
            FilePath testDir = new FilePath(testDirPath);

            testDir.Mkdirs();
            System.Console.Out.WriteLine(testDir.GetAbsolutePath());
            FilePath inFile  = new FilePath(testDir, "fileIn.out");
            FilePath outFile = new FilePath(testDir, "fileOut.out");

            //Initialize input file
            byte[] initBuff = new byte[FileSize];
            Random rand     = new Random();

            rand.NextBytes(initBuff);
            FileOutputStream @out = new FileOutputStream(inFile);

            try
            {
                @out.Write(initBuff);
            }
            finally
            {
                IOUtils.Cleanup(Log, @out);
            }
            //define position and count to read from a file region.
            int position = 2 * 1024 * 1024;
            int count    = 4 * 1024 * 1024 - 1;
            RandomAccessFile    inputFile  = null;
            RandomAccessFile    targetFile = null;
            WritableByteChannel target     = null;
            FadvisedFileRegion  fileRegion = null;

            try
            {
                inputFile  = new RandomAccessFile(inFile.GetAbsolutePath(), "r");
                targetFile = new RandomAccessFile(outFile.GetAbsolutePath(), "rw");
                target     = targetFile.GetChannel();
                NUnit.Framework.Assert.AreEqual(FileSize, inputFile.Length());
                //create FadvisedFileRegion
                fileRegion = new FadvisedFileRegion(inputFile, position, count, false, 0, null, null
                                                    , 1024, false);
                //test corner cases
                CustomShuffleTransferCornerCases(fileRegion, target, count);
                long pos = 0;
                long size;
                while ((size = fileRegion.CustomShuffleTransfer(target, pos)) > 0)
                {
                    pos += size;
                }
                //assert size
                NUnit.Framework.Assert.AreEqual(count, (int)pos);
                NUnit.Framework.Assert.AreEqual(count, targetFile.Length());
            }
            finally
            {
                if (fileRegion != null)
                {
                    fileRegion.ReleaseExternalResources();
                }
                IOUtils.Cleanup(Log, target);
                IOUtils.Cleanup(Log, targetFile);
                IOUtils.Cleanup(Log, inputFile);
            }
            //Read the target file and verify that copy is done correctly
            byte[]          buff = new byte[FileSize];
            FileInputStream @in  = new FileInputStream(outFile);

            try
            {
                int total = @in.Read(buff, 0, count);
                NUnit.Framework.Assert.AreEqual(count, total);
                for (int i = 0; i < count; i++)
                {
                    NUnit.Framework.Assert.AreEqual(initBuff[position + i], buff[i]);
                }
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
            }
            //delete files and folders
            inFile.Delete();
            outFile.Delete();
            testDir.Delete();
            absLogDir.Delete();
        }
 public BufferedChannelOutput(WritableByteChannel channel) : this(channel, 1024)
 {
 }
 public BufferedChannelOutput(WritableByteChannel channel, int bufferSize) : this( bufferSize )
 {
     Reset(channel);
 }
Exemple #15
0
 public static Writer newWriter(WritableByteChannel arg0, CharsetEncoder arg1, int arg2)
 {
     return Static.CallMethod<Writer>(typeof(Channels), "newWriter", "(Ljava/nio/channels/WritableByteChannel;Ljava/nio/charset/CharsetEncoder;I)Ljava/io/Writer;", arg0, arg1, arg2);
 }
Exemple #16
0
 public virtual long transferTo(long prm1, long prm2, WritableByteChannel prm3)
 {
     return(default(long));
 }
Exemple #17
0
 public static OutputStream newOutputStream(WritableByteChannel arg0)
 {
     return Static.CallMethod<OutputStream>(typeof(Channels), "newOutputStream", "(Ljava/nio/channels/WritableByteChannel;)Ljava/io/OutputStream;", arg0);
 }
Exemple #18
0
 public static Writer newWriter(WritableByteChannel arg0, String arg1)
 {
     return Static.CallMethod<Writer>(typeof(Channels), "newWriter", "(Ljava/nio/channels/WritableByteChannel;Ljava/lang/String;)Ljava/io/Writer;", arg0, arg1);
 }
Exemple #19
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long writeData(java.nio.channels.ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData, java.nio.channels.WritableByteChannel channel) throws java.io.IOException
        private long WriteData(ReadableByteChannel data, ByteBuffer temporaryBuffer, bool hasData, WritableByteChannel channel)
        {
            long totalToWrite = 0;
            long totalWritten = 0;

            if (hasData)
            {
                while (data.read(temporaryBuffer) >= 0)
                {
                    temporaryBuffer.flip();
                    totalToWrite += temporaryBuffer.limit();
                    int bytesWritten;
                    while ((totalWritten += bytesWritten = channel.write(temporaryBuffer)) < totalToWrite)
                    {
                        if (bytesWritten < 0)
                        {
                            throw new IOException("Unable to write to disk, reported bytes written was " + bytesWritten);
                        }
                    }
                    temporaryBuffer.clear();
                }
            }
            return(totalWritten);
        }
Exemple #20
0
        /// <summary>
        /// Constructs a writer that encodes characters using the given encoder and
        /// writes the resulting bytes to the given channel.
        ///
        /// <para> The resulting stream will contain an internal output buffer of at
        /// least <tt>minBufferCap</tt> bytes.  The stream's <tt>write</tt> methods
        /// will, as needed, flush the buffer by writing bytes to the underlying
        /// channel; if the channel is in non-blocking mode when bytes are to be
        /// written then an <seealso cref="IllegalBlockingModeException"/> will be thrown.
        /// The resulting stream will not otherwise be buffered.  Closing the stream
        /// will in turn cause the channel to be closed.  </para>
        /// </summary>
        /// <param name="ch">
        ///         The channel to which bytes will be written
        /// </param>
        /// <param name="enc">
        ///         The charset encoder to be used
        /// </param>
        /// <param name="minBufferCap">
        ///         The minimum capacity of the internal byte buffer,
        ///         or <tt>-1</tt> if an implementation-dependent
        ///         default capacity is to be used
        /// </param>
        /// <returns>  A new writer </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
//ORIGINAL LINE: public static java.io.Writer newWriter(final WritableByteChannel ch, final java.nio.charset.CharsetEncoder enc, final int minBufferCap)
        public static Writer NewWriter(WritableByteChannel ch, CharsetEncoder enc, int minBufferCap)
        {
            CheckNotNull(ch, "ch");
            return(StreamEncoder.forEncoder(ch, enc.Reset(), minBufferCap));
        }
Exemple #21
0
 /// <summary>
 /// Constructs a writer that encodes characters according to the named
 /// charset and writes the resulting bytes to the given channel.
 ///
 /// <para> An invocation of this method of the form
 ///
 /// <blockquote><pre>
 /// Channels.newWriter(ch, csname)</pre></blockquote>
 ///
 /// behaves in exactly the same way as the expression
 ///
 /// <blockquote><pre>
 /// Channels.newWriter(ch,
 ///                    Charset.forName(csName)
 ///                        .newEncoder(),
 ///                    -1);</pre></blockquote>
 ///
 /// </para>
 /// </summary>
 /// <param name="ch">
 ///         The channel to which bytes will be written
 /// </param>
 /// <param name="csName">
 ///         The name of the charset to be used
 /// </param>
 /// <returns>  A new writer
 /// </returns>
 /// <exception cref="UnsupportedCharsetException">
 ///          If no support for the named charset is available
 ///          in this instance of the Java virtual machine </exception>
 public static Writer NewWriter(WritableByteChannel ch, String csName)
 {
     CheckNotNull(csName, "csName");
     return(NewWriter(ch, Charset.ForName(csName).NewEncoder(), -1));
 }
Exemple #22
0
 /// <exception cref="System.IO.IOException"/>
 internal Writer(WritableByteChannel channel, long timeout)
     : base((SelectableChannel)channel, timeout)
 {
     this.channel = channel;
 }
Exemple #23
0
 internal Machine(int bufferSize)
 {
     this.OutputConflict = new MemoryStream();
     this.Writable       = Channels.newChannel(this.OutputConflict);
     this.PackerConflict = new PackStream.Packer(new BufferedChannelOutput(this.Writable, bufferSize));
 }
Exemple #24
0
 /// <summary>Create a new ouput stream with the given timeout.</summary>
 /// <remarks>
 /// Create a new ouput stream with the given timeout. If the timeout
 /// is zero, it will be treated as infinite timeout. The socket's
 /// channel will be configured to be non-blocking.
 /// </remarks>
 /// <param name="channel">
 ///
 /// Channel for writing, should also be a
 /// <see cref="SelectableChannel"/>
 /// .
 /// The channel will be configured to be non-blocking.
 /// </param>
 /// <param name="timeout">timeout in milliseconds. must not be negative.</param>
 /// <exception cref="System.IO.IOException"/>
 public SocketOutputStream(WritableByteChannel channel, long timeout)
 {
     SocketIOWithTimeout.CheckChannelValidity(channel);
     writer = new SocketOutputStream.Writer(channel, timeout);
 }
Exemple #25
0
        /// <summary>
        /// Constructs a stream that writes bytes to the given channel.
        ///
        /// <para> The <tt>write</tt> methods of the resulting stream will throw an
        /// <seealso cref="IllegalBlockingModeException"/> if invoked while the underlying
        /// channel is in non-blocking mode.  The stream will not be buffered.  The
        /// stream will be safe for access by multiple concurrent threads.  Closing
        /// the stream will in turn cause the channel to be closed.  </para>
        /// </summary>
        /// <param name="ch">
        ///         The channel to which bytes will be written
        /// </param>
        /// <returns>  A new output stream </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET:
//ORIGINAL LINE: public static java.io.OutputStream newOutputStream(final WritableByteChannel ch)
        public static OutputStream NewOutputStream(WritableByteChannel ch)
        {
            CheckNotNull(ch, "ch");

            return(new OutputStreamAnonymousInnerClassHelper(ch));
        }