Ejemplo n.º 1
0
 public byte[] ToArray()
 {
     java.nio.ByteBuffer dup = this.buffer.duplicate();
     byte[] result           = new byte[this.size];
     dup.position(this.offset);
     dup.get(result, 0, this.size);
     return(result);
 }
Ejemplo n.º 2
0
        public static short getShortNumeric(java.nio.ByteBuffer buffer, int len)
        {
            String s = "";

            if (null != buffer && buffer.remaining() >= len)
            {
                byte[] dest = new byte[len];
                buffer.get(dest, 0, len);
                s = new String(dest);
            }
            return((short)(0xFFFF & Integer.parseInt(s)));
        }
Ejemplo n.º 3
0
        public static String getString(java.nio.ByteBuffer buffer, int len)
        {
            String s = "";

            if (null != buffer && buffer.remaining() >= len)
            {
                byte[] dest = new byte[len];
                buffer.get(dest, 0, len);
                s = new String(dest).trim();
            }
            return(s);
        }
Ejemplo n.º 4
0
        public static String getString(java.nio.ByteBuffer buffer, int offset, int len)
        {
            String s = "";

            if (null != buffer && buffer.capacity() >= offset + len)
            {
                byte[] dest = new byte[len];
                buffer.position(offset);
                buffer.get(dest, 0, len);
                s = new String(dest).trim();
            }
            return(s);
        }
Ejemplo n.º 5
0
 public RPFHeaderSection(java.nio.ByteBuffer buffer)
 {
     this.endianIndicator         = ((byte)0 != buffer.get());        // reads 1 byte, 0 for big endian
     this.headerLength            = buffer.getShort();                // reads 2 bytes
     this.filename                = NITFSUtil.getString(buffer, 12);
     this.updateIndicator         = NITFSUtil.getByteAsShort(buffer); // reads 1 byte (short)
     this.govSpecNumber           = NITFSUtil.getString(buffer, 15);
     this.govSpecDate             = NITFSUtil.getString(buffer, 8);
     this.securityClass           = NITFSUtil.getString(buffer, 1);
     this.securityCountryCode     = NITFSUtil.getString(buffer, 2);
     this.securityReleaseMark     = NITFSUtil.getString(buffer, 2);
     this.locationSectionLocation = buffer.getInt();        // read 4 bytes (int)
 }
Ejemplo n.º 6
0
        /// <summary>Pretty-print byte buffer in hex</summary>
        /// <param name="buffer"/>
        /// <returns/>
        public static string print(java.nio.ByteBuffer buffer)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int lim = buffer.limit();

            for (int i = 0; i < lim; i++)
            {
                sb.Append(string.format("%x", buffer.get(i)));
                if (i % 8 == 7)
                {
                    sb.Append(" ");
                }
            }
            return(sb.ToString().Trim());
        }
Ejemplo n.º 7
0
 public override string ToString()
 {
     byte[] bytes            = new byte[this.size];
     java.nio.ByteBuffer dup = this.buffer.duplicate();
     dup.position(this.offset);
     dup.get(bytes, 0, this.size);
     try
     {
         return(Sharpen.Runtime.GetStringForBytes(bytes, "UTF-8"));
     }
     catch (System.Exception)
     {
         throw new System.Exception("UTF-8 is unsupported");
     }
 }
Ejemplo n.º 8
0
        private void loadColorMaps(java.nio.ByteBuffer buffer, int colormapSubsectionOffset)
        {
            if (0 == this.numOfColorRecords)
            {
                throw new NITFSRuntimeException("NITFSReader.InvalidNumberOfColorRecords");
            }
            if (0 == this.colorElementLength)
            {
                throw new NITFSRuntimeException("NITFSReader.InvalidLengthOfColorRecordElement");
            }

            buffer.position((int)(colormapSubsectionOffset + this.colorTableOffset));
            int mapLength = (int)(this.numOfColorRecords * this.colorElementLength);

            this.colorMap = new byte[mapLength];
            buffer.get(this.colorMap, 0, mapLength);
        }
Ejemplo n.º 9
0
        public NITFSImageBand(java.nio.ByteBuffer buffer)
        {
            this.representation = NITFSUtil.getString(buffer, 2);
            this.significanceForImageCategory = NITFSUtil.getString(buffer, 6);
            this.imageFilterCondition         = NITFSUtil.getString(buffer, 1);
            this.stdImageFilterCode           = NITFSUtil.getString(buffer, 3);
            this.numOfLookupTables            = NITFSUtil.getShortNumeric(buffer, 1);
            this.numOfLookupTableEntries      = NITFSUtil.getShortNumeric(buffer, 5);
            if (0 < this.numOfLookupTables && 0 < this.numOfLookupTableEntries)
            {
                this.lut = new byte[this.numOfLookupTables][this.numOfLookupTableEntries];
                for (int j = 0; j < this.numOfLookupTables; j++)
                {
                    buffer.get(this.lut[j], 0, this.numOfLookupTableEntries);
                }
            }

            this.isGrayImage         = (1 == this.numOfLookupTables);
            this.hasTransparentEntry = (217 == this.numOfLookupTableEntries);
        }
        public CompressionLookupRecord(java.nio.ByteBuffer buffer,
                                       int compressionLookupSubsectionLocation,
                                       RPFColorMap[] colormaps // TODO update LUT with the color mapped values to gain performance
                                       )

        {
            this.tableID              = NITFSUtil.getUShort(buffer);
            this.numOfRecords         = (int)NITFSUtil.getUInt(buffer);
            this.numOfValuesPerRecord = NITFSUtil.getUShort(buffer);
            this.valueBitLength       = NITFSUtil.getUShort(buffer);
            this.tableLocation        = (int)(NITFSUtil.getUInt(buffer) + compressionLookupSubsectionLocation);
            int saveOffset = buffer.position();

            this.bytesPerRecord = (short)(this.numOfValuesPerRecord * this.valueBitLength / 8L);
            this.lut            = new byte[this.numOfRecords * this.bytesPerRecord];

            buffer.position(this.tableLocation);
            buffer.get(this.lut, 0, this.numOfRecords * this.bytesPerRecord);

            buffer.position(saveOffset);
        }
Ejemplo n.º 11
0
        public static String getBitString(java.nio.ByteBuffer buffer, int lenBits)
        {
            String s   = "";
            int    len = (int)Math.Ceiling(lenBits / (double)Byte.SIZE);

            if (null != buffer && buffer.remaining() >= len)
            {
                byte[] dest = new byte[len];
                buffer.get(dest, 0, len);

                char[] bits = new char[lenBits];
                for (int i = 0; i < lenBits; i++)
                {
                    int mask = 0x1 << (Byte.SIZE - (i % Byte.SIZE) - 1);
                    // U+0030 : unicode zero
                    // U+0031 : unicode one
                    bits[i] = (mask & dest[i / Byte.SIZE]) == 0 ? '\u0030' : '\u0031';
                }
                s = new String(bits);
            }
            return(s);
        }
Ejemplo n.º 12
0
        public static void BlockCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length)
        {
            if (src.hasArray() && dst.hasArray())
            {
                java.lang.System.arraycopy(src.array(),
                                           src.arrayOffset() + srcPos,
                                           dst.array(),
                                           dst.arrayOffset() + dstPos,
                                           length);
            }
            else
            {
                if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }

                for (int i = 0; i < length; i++)
                {
                    // TODO: ByteBuffer.put is polymorphic, and might be slow here
                    dst.put(dstPos++, src.get(srcPos++));
                }
            }
        }
Ejemplo n.º 13
0
        /// <exception cref="System.IO.IOException"/>
        public int Read(java.nio.ByteBuffer outBuf)
        {
            if (outBuf.buffer == null)
            {
                outBuf.buffer = new byte[outBuf.remaining()];
            }
            int len = outBuf.remaining();

            if (len == 0)
            {
                return(0);
            }
            if (len % 8 != 0)
            {
                throw new System.Exception("PackedInputStream reads must be word-aligned");
            }
            int outPtr = outBuf.position();
            int outEnd = outPtr + len;

            java.nio.ByteBuffer inBuf = this.inner.GetReadBuffer();
            while (true)
            {
                byte tag = 0;
                if (inBuf.remaining() < 10)
                {
                    if (outBuf.remaining() == 0)
                    {
                        return(len);
                    }
                    if (inBuf.remaining() == 0)
                    {
                        inBuf = this.inner.GetReadBuffer();
                        continue;
                    }
                    //# We have at least 1, but not 10, bytes available. We need to read
                    //# slowly, doing a bounds check on each byte.
                    tag = inBuf.get();
                    for (int i = 0; i < 8; ++i)
                    {
                        if ((tag & (1 << i)) != 0)
                        {
                            if (inBuf.remaining() == 0)
                            {
                                inBuf = this.inner.GetReadBuffer();
                            }
                            outBuf.put(inBuf.get());
                        }
                        else
                        {
                            outBuf.put((byte)0);
                        }
                    }
                    if (inBuf.remaining() == 0 && (tag == 0 || tag == 0xff))
                    {
                        inBuf = this.inner.GetReadBuffer();
                    }
                }
                else
                {
                    tag = inBuf.get();
                    for (int n = 0; n < 8; ++n)
                    {
                        bool isNonzero = (tag & (1 << n)) != 0;
                        outBuf.put(unchecked ((byte)(inBuf.get() & (isNonzero? -1 : 0))));
                        inBuf.position(inBuf.position() + (isNonzero? 0 : -1));
                    }
                }
                if (tag == 0)
                {
                    if (inBuf.remaining() == 0)
                    {
                        throw new System.Exception("Should always have non-empty buffer here.");
                    }
                    int runLength = (unchecked ((int)(0xff)) & (int)inBuf.get()) * 8;
                    if (runLength > outEnd - outPtr)
                    {
                        throw new System.Exception("Packed input did not end cleanly on a segment boundary");
                    }
                    for (int i = 0; i < runLength; ++i)
                    {
                        outBuf.put((byte)0);
                    }
                }
                else if (tag == 0xff)
                {
                    int runLength = (unchecked ((int)(0xff)) & (int)inBuf.get()) * 8;
                    if (inBuf.remaining() >= runLength)
                    {
                        //# Fast path.
                        java.nio.ByteBuffer slice = inBuf.slice();
                        slice.limit(runLength);
                        outBuf.put(slice);
                        inBuf.position(inBuf.position() + runLength);
                    }
                    else
                    {
                        //# Copy over the first buffer, then do one big read for the rest.
                        runLength -= inBuf.remaining();
                        outBuf.put(inBuf);
                        java.nio.ByteBuffer slice = outBuf.slice();
                        slice.limit(runLength);
                        this.inner.Read(slice);
                        outBuf.position(outBuf.position() + runLength);
                        if (outBuf.remaining() == 0)
                        {
                            return(len);
                        }
                        inBuf = this.inner.GetReadBuffer();
                        continue;
                    }
                }
                if (outBuf.remaining() == 0)
                {
                    return(len);
                }
            }
        }
Ejemplo n.º 14
0
        /// <exception cref="System.IO.IOException"/>
        public int Write(java.nio.ByteBuffer inBuf)
        {
            int length = inBuf.remaining();

            java.nio.ByteBuffer @out       = this.inner.GetWriteBuffer();
            java.nio.ByteBuffer slowBuffer = java.nio.ByteBuffer.allocate(20);
            int inPtr = inBuf.position();
            int inEnd = inPtr + length;

            while (inPtr < inEnd)
            {
                if (@out.remaining() < 10)
                {
                    //# Oops, we're out of space. We need at least 10
                    //# bytes for the fast path, since we don't
                    //# bounds-check on every byte.
                    if (@out == slowBuffer)
                    {
                        int oldLimit = @out.limit();
                        @out.limit(@out.position());
                        @out.rewind();
                        this.inner.Write(@out);
                        @out.limit(oldLimit);
                    }
                    @out = slowBuffer;
                    @out.rewind();
                }
                int tagPos = @out.position();
                @out.position(tagPos + 1);
                byte curByte;
                curByte = inBuf.get(inPtr);
                byte bit0 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit0 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit1 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit1 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit2 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit2 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit3 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit3 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit4 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit4 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit5 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit5 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit6 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit6 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit7 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit7 - 1);
                inPtr += 1;
                byte tag = unchecked ((byte)((bit0 << 0) | (bit1 << 1) | (bit2 << 2) | (bit3 << 3)
                                             | (bit4 << 4) | (bit5 << 5) | (bit6 << 6) | (bit7 << 7)));
                @out.put(tagPos, tag);
                if (tag == 0)
                {
                    //# An all-zero word is followed by a count of
                    //# consecutive zero words (not including the first
                    //# one).
                    int runStart = inPtr;
                    int limit    = inEnd;
                    if (limit - inPtr > 255 * 8)
                    {
                        limit = inPtr + 255 * 8;
                    }
                    while (inPtr < limit && inBuf.getLong(inPtr) == 0)
                    {
                        inPtr += 8;
                    }
                    @out.put(unchecked ((byte)((inPtr - runStart) / 8)));
                }
                else if (tag == unchecked ((byte)unchecked ((int)(0xff))))
                {
                    //# An all-nonzero word is followed by a count of
                    //# consecutive uncompressed words, followed by the
                    //# uncompressed words themselves.
                    //# Count the number of consecutive words in the input
                    //# which have no more than a single zero-byte. We look
                    //# for at least two zeros because that's the point
                    //# where our compression scheme becomes a net win.
                    int runStart = inPtr;
                    int limit    = inEnd;
                    if (limit - inPtr > 255 * 8)
                    {
                        limit = inPtr + 255 * 8;
                    }
                    while (inPtr < limit)
                    {
                        byte c = 0;
                        for (int ii = 0; ii < 8; ++ii)
                        {
                            c     += (inBuf.get(inPtr) == 0 ? (byte)1 : (byte)0);
                            inPtr += 1;
                        }
                        if (c >= 2)
                        {
                            //# Un-read the word with multiple zeros, since
                            //# we'll want to compress that one.
                            inPtr -= 8;
                            break;
                        }
                    }
                    int count = inPtr - runStart;
                    @out.put(unchecked ((byte)(count / 8)));
                    if (count <= @out.remaining())
                    {
                        //# There's enough space to memcpy.
                        inBuf.position(runStart);
                        java.nio.ByteBuffer slice = inBuf.slice();
                        slice.limit(count);
                        @out.put(slice);
                    }
                    else
                    {
                        //# Input overruns the output buffer. We'll give it
                        //# to the output stream in one chunk and let it
                        //# decide what to do.
                        if (@out == slowBuffer)
                        {
                            int oldLimit = @out.limit();
                            @out.limit(@out.position());
                            @out.rewind();
                            this.inner.Write(@out);
                            @out.limit(oldLimit);
                        }
                        inBuf.position(runStart);
                        java.nio.ByteBuffer slice = inBuf.slice();
                        slice.limit(count);
                        while (slice.hasRemaining())
                        {
                            this.inner.Write(slice);
                        }
                        @out = this.inner.GetWriteBuffer();
                    }
                }
            }
            if (@out == slowBuffer)
            {
                @out.limit(@out.position());
                @out.rewind();
                this.inner.Write(@out);
            }
            inBuf.position(inPtr);
            return(length);
        }
Ejemplo n.º 15
0
 /// <seealso>[Hitchens2002] p.47: Accessing Unsigned Data</seealso>
 /// <param name="buffer"/>
 /// <returns/>
 public static short getUnsignedByte(java.nio.ByteBuffer buffer)
 {
     return((short)(buffer.get() & (short)unchecked ((int)(0xff))));
 }
Ejemplo n.º 16
0
 public static bool getBoolean(java.nio.ByteBuffer buffer)
 {
     return(!((byte)0 == buffer.get())); // 0 = false, non-zero = true
 }
Ejemplo n.º 17
0
 public static short getByteAsShort(java.nio.ByteBuffer buffer)
 {
     return((short)(0xFF & buffer.get()));
 }