2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using Document
= Lucene
.Net
.Documents
.Document
;
18 using Field
= Lucene
.Net
.Documents
.Field
;
19 using Directory
= Lucene
.Net
.Store
.Directory
;
20 using IndexOutput
= Lucene
.Net
.Store
.IndexOutput
;
21 namespace Lucene
.Net
.Index
24 sealed class FieldsWriter
26 internal const byte FIELD_IS_TOKENIZED
= (byte) (0x1);
27 internal const byte FIELD_IS_BINARY
= (byte) (0x2);
28 internal const byte FIELD_IS_COMPRESSED
= (byte) (0x4);
30 private FieldInfos fieldInfos
;
32 private IndexOutput fieldsStream
;
34 private IndexOutput indexStream
;
36 internal FieldsWriter(Directory d
, System
.String segment
, FieldInfos fn
)
39 fieldsStream
= d
.CreateOutput(segment
+ ".fdt");
40 indexStream
= d
.CreateOutput(segment
+ ".fdx");
49 internal void AddDocument(Document doc
)
51 indexStream
.WriteLong(fieldsStream
.GetFilePointer());
54 foreach (Field field
in doc
.Fields())
59 fieldsStream
.WriteVInt(storedCount
);
61 foreach (Field field
in doc
.Fields())
65 fieldsStream
.WriteVInt(fieldInfos
.FieldNumber(field
.Name()));
68 if (field
.IsTokenized())
69 bits
|= FieldsWriter
.FIELD_IS_TOKENIZED
;
71 bits
|= FieldsWriter
.FIELD_IS_BINARY
;
72 if (field
.IsCompressed())
73 bits
|= FieldsWriter
.FIELD_IS_COMPRESSED
;
75 fieldsStream
.WriteByte(bits
);
77 if (field
.IsCompressed())
79 // compression is enabled for the current field
81 // check if it is a binary field
84 data
= Compress(field
.BinaryValue());
88 byte[] encodingByteArray
= System
.Text
.Encoding
.GetEncoding("UTF-8").GetBytes(field
.StringValue());
89 byte[] byteArray
= new byte[encodingByteArray
.Length
];
90 for (int ii
= 0; ii
< encodingByteArray
.Length
; ii
++)
91 byteArray
[ii
] = (byte) encodingByteArray
[ii
];
93 data
= Compress(byteArray
);
95 int len
= data
.Length
;
96 fieldsStream
.WriteVInt(len
);
97 fieldsStream
.WriteBytes(data
, len
);
101 // compression is disabled for the current field
102 if (field
.IsBinary())
104 byte[] data
= field
.BinaryValue();
105 int len
= data
.Length
;
106 fieldsStream
.WriteVInt(len
);
107 fieldsStream
.WriteBytes(data
, len
);
111 fieldsStream
.WriteString(field
.StringValue());
118 private byte[] Compress(byte[] input
)
120 // {{Aroush-1.9}} how can we do this in .NET
124 // Create the compressor with highest level of compression
125 //UPGRADE_ISSUE: Class 'java.util.zip.Deflater' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
126 //UPGRADE_ISSUE: Constructor 'java.util.zip.Deflater.Deflater' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
127 Deflater compressor = new Deflater();
128 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.setLevel' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
129 //UPGRADE_ISSUE: Field 'java.util.zip.Deflater.BEST_COMPRESSION' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
130 compressor.setLevel(Deflater.BEST_COMPRESSION);
132 // Give the compressor the data to compress
133 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.setInput' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
134 compressor.setInput(input);
135 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.finish' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
139 * Create an expandable byte array to hold the compressed data.
140 * You cannot use an array that's the same size as the orginal because
141 * there is no guarantee that the compressed data will be smaller than
142 * the uncompressed data.
144 System.IO.MemoryStream bos = new System.IO.MemoryStream(input.Length);
147 byte[] buf = new byte[1024];
148 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.finished' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
149 while (!compressor.finished())
151 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.deflate' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
152 int count = compressor.deflate(buf);
153 bos.Write(SupportClass.ToByteArray(buf), 0, count);
156 //UPGRADE_ISSUE: Method 'java.util.zip.Deflater.end' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javautilzipDeflater_3"'
159 // Get the compressed data
160 return SupportClass.ToSByteArray(bos.ToArray());