CSJ2K.j2k.io.BufferedRandomAccessFile.readFully C# (CSharp) Method

readFully() public method

Reads up to len bytes of data from this file into an array of bytes. This method reads repeatedly from the stream until all the bytes are read. This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.
If the end-of file was reached before /// getting all the necessary data. /// /// If an I/O error ocurred. /// ///
public readFully ( byte b, int off, int len ) : void
b byte The buffer into which the data is to be read. It must be long /// enough. /// ///
off int The index in 'b' where to place the first byte read. /// ///
len int The number of bytes to read. /// ///
return void
        public void readFully(byte[] b, int off, int len)
        {
            int clen; // current length to read
            while (len > 0)
            {
                // There still is some data to read
                if (position < maxByte)
                {
                    // We can read some data from buffer
                    clen = maxByte - position;
                    if (clen > len)
                        clen = len;
                    Array.Copy(byteBuffer, position, b, off, clen);
                    position += clen;
                    off += clen;
                    len -= clen;
                }
                else if (isEOFInBuffer)
                {
                    position = maxByte + 1; // Set position to EOF
                    throw new System.IO.EndOfStreamException();
                }
                else
                {
                    // Buffer empty => get more data
                    readNewBuffer(offset + position);
                }
            }
        }

Usage Example

Example #1
0
        /// <summary> This method reads and buffers the tile headers, packet headers and
        /// packet data.
        /// 
        /// </summary>
        /// <param name="fi">The file to read the headers and data from
        /// 
        /// </param>
        /// <exception cref="IOException">If an I/O error ocurred.
        /// 
        /// </exception>
        private void readAndBuffer(BufferedRandomAccessFile fi)
        {
            int p, prem, length, t, markIndex;

            // Buffer main header
            fi.seek(0);
            length = ((System.Int32) positions[0]) - 2;
            mainHeader = new byte[length];
            fi.readFully(mainHeader, 0, length);
            markIndex = 0;

            for (t = 0; t < nt; t++)
            {
                prem = ppt[t];

                packetHeaders[t] = new byte[prem][];
                packetData[t] = new byte[prem][];
                sopMarkSeg[t] = new byte[prem][];

                // Read tile header
                length = positions[markIndex + 1] - positions[markIndex];
                tileHeaders[t] = new byte[length];
                fi.readFully(tileHeaders[t], 0, length);
                markIndex++;

                for (p = 0; p < prem; p++)
                {
                    // Read packet header
                    length = positions[markIndex + 1] - positions[markIndex];

                    if (tempSop)
                    {
                        // SOP marker is skipped
                        length -= CSJ2K.j2k.codestream.Markers.SOP_LENGTH;
                        fi.skipBytes(CSJ2K.j2k.codestream.Markers.SOP_LENGTH);
                    }
                    else
                    {
                        // SOP marker is read and buffered
                        length -= CSJ2K.j2k.codestream.Markers.SOP_LENGTH;
                        sopMarkSeg[t][p] = new byte[CSJ2K.j2k.codestream.Markers.SOP_LENGTH];
                        fi.readFully(sopMarkSeg[t][p], 0, CSJ2K.j2k.codestream.Markers.SOP_LENGTH);
                    }

                    if (!tempEph)
                    {
                        // EPH marker is kept in header
                        length += CSJ2K.j2k.codestream.Markers.EPH_LENGTH;
                    }
                    packetHeaders[t][p] = new byte[length];
                    fi.readFully(packetHeaders[t][p], 0, length);
                    markIndex++;

                    // Read packet data
                    length = positions[markIndex + 1] - positions[markIndex];

                    length -= CSJ2K.j2k.codestream.Markers.EPH_LENGTH;
                    if (tempEph)
                    {
                        // EPH marker is used and is skipped
                        fi.skipBytes(CSJ2K.j2k.codestream.Markers.EPH_LENGTH);
                    }

                    packetData[t][p] = new byte[length];
                    fi.readFully(packetData[t][p], 0, length);
                    markIndex++;
                }
            }
        }