public void BufferedReaderWriter_Basics()
        {
            Random r = new Random(5);

            byte[] content = new byte[1024];
            r.NextBytes(content);

            byte[] result = new byte[128];

            using (BufferedReader reader = BufferedReader.FromArray(content, 0, content.Length))
                using (BufferedWriter writer = BufferedWriter.ToArray(content))
                {
                    while (!reader.EndOfStream)
                    {
                        int length = reader.EnsureSpace(150);
                        writer.EnsureSpace(length);

                        Buffer.BlockCopy(reader.Buffer, reader.Index, writer.Buffer, writer.Index, length);

                        reader.Index += length;
                        writer.Index += length;
                    }

                    result = writer.Buffer;
                }

            Assert.IsTrue(result.Length >= content.Length);
            for (int i = 0; i < content.Length; ++i)
            {
                Assert.AreEqual(content[i], result[i], $"@{i:n0}, expect: {content[i]}, actual: {result[i]}");
            }
        }
예제 #2
0
        public void RewriteOptimized(BufferedWriter writer, string containerIndexPath = null, string searchIndexPath = null)
        {
            int[] map = _compressor.OptimizeIndex();

            using (BufferedReader inner = BufferedReader.FromArray(_reader.Buffer, 0, 0))
                using (ContainerIndex containerIndex = (containerIndexPath == null ? null : ContainerIndex.OpenWrite(containerIndexPath)))
                    using (SearchIndexWriter indexWriter = (searchIndexPath == null ? null : new SearchIndexWriter(searchIndexPath, map.Length, 128 * 1024)))
                    {
                        long last = 0;

                        while (this.Read())
                        {
                            int length = (int)(this.BytesRead - last);
                            writer.EnsureSpace(length);

                            if (LengthLookup[(byte)_currentMarker] >= 0)
                            {
                                // Everything but compressed text: write bytes out
                                Buffer.BlockCopy(_reader.Buffer, _reader.Index - length, writer.Buffer, writer.Index, length);
                                writer.Index += length;
                            }
                            else
                            {
                                writer.Buffer[writer.Index++] = (byte)_currentMarker;

                                // Compressed Test: Rewrite the text segment
                                inner.ReadSlice(_reader.Buffer, _reader.Index - _currentLength, _reader.Index - 1);
                                _compressor.RewriteOptimized(map, inner, writer, indexWriter);

                                writer.Buffer[writer.Index++] = (byte)BionMarker.EndValue;
                            }

                            if ((byte)_currentMarker >= (byte)BionMarker.EndArray)
                            {
                                if ((byte)_currentMarker >= (byte)BionMarker.StartArray)
                                {
                                    containerIndex?.Start(writer.BytesWritten - 1);
                                }
                                else
                                {
                                    containerIndex?.End(writer.BytesWritten);
                                }
                            }

                            last = this.BytesRead;
                        }
                    }
        }
예제 #3
0
        private void DecompressString()
        {
            if (_decompressReader == null)
            {
                _decompressReader = BufferedReader.FromArray(_reader.Buffer, 0, 0);
                _decompressWriter = BufferedWriter.ToArray(new byte[1024]);
            }

            _decompressReader.ReadSlice(_reader.Buffer, _currentCompressedStringStart, _reader.Index - 1);
            _decompressWriter.Index = 0;

            // Decompress the content
            _compressor.Expand(_decompressReader, _decompressWriter);

            // Make a String8 referencing the full decompressed value
            _currentString = String8.Reference(_decompressWriter.Buffer, 0, _decompressWriter.Index);
        }