All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.store.DataOutput Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * COPIED FROM APACHE LUCENE 4.7.2
 *
 * Git URL: [email protected]:apache/lucene.git, tag: releases/lucene-solr/4.7.2, path: lucene/core/src/java
 *
 * (see https://issues.apache.org/jira/browse/OAK-10786 for details)
 */

package org.apache.lucene.store;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;

/**
 * Abstract base class for performing write operations of Lucene's low-level
 * data types.
 
 * 

{@code DataOutput} may only be used from one thread, because it is not * thread safe (it keeps internal state like file position). */ public abstract class DataOutput { /** Writes a single byte. *

* The most primitive data type is an eight-bit byte. Files are * accessed as sequences of bytes. All other data types are defined * as sequences of bytes, so file formats are byte-order independent. * * @see IndexInput#readByte() */ public abstract void writeByte(byte b) throws IOException; /** Writes an array of bytes. * @param b the bytes to write * @param length the number of bytes to write * @see DataInput#readBytes(byte[],int,int) */ public void writeBytes(byte[] b, int length) throws IOException { writeBytes(b, 0, length); } /** Writes an array of bytes. * @param b the bytes to write * @param offset the offset in the byte array * @param length the number of bytes to write * @see DataInput#readBytes(byte[],int,int) */ public abstract void writeBytes(byte[] b, int offset, int length) throws IOException; /** Writes an int as four bytes. *

* 32-bit unsigned integer written as four bytes, high-order bytes first. * * @see DataInput#readInt() */ public void writeInt(int i) throws IOException { writeByte((byte)(i >> 24)); writeByte((byte)(i >> 16)); writeByte((byte)(i >> 8)); writeByte((byte) i); } /** Writes a short as two bytes. * @see DataInput#readShort() */ public void writeShort(short i) throws IOException { writeByte((byte)(i >> 8)); writeByte((byte) i); } /** Writes an int in a variable-length format. Writes between one and * five bytes. Smaller values take fewer bytes. Negative numbers are * supported, but should be avoided. *

VByte is a variable-length format for positive integers is defined where the * high-order bit of each byte indicates whether more bytes remain to be read. The * low-order seven bits are appended as increasingly more significant bits in the * resulting integer value. Thus values from zero to 127 may be stored in a single * byte, values from 128 to 16,383 may be stored in two bytes, and so on.

*

VByte Encoding Example

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ValueByte 1Byte 2Byte 3
000000000
100000001
200000010
...
12701111111
1281000000000000001
1291000000100000001
1301000001000000001
...
16,3831111111101111111
16,384100000001000000000000001
16,385100000011000000000000001
...
*

This provides compression while still being efficient to decode.

* * @param i Smaller values take fewer bytes. Negative numbers are * supported, but should be avoided. * @throws IOException If there is an I/O error writing to the underlying medium. * @see DataInput#readVInt() */ public final void writeVInt(int i) throws IOException { while ((i & ~0x7F) != 0) { writeByte((byte)((i & 0x7F) | 0x80)); i >>>= 7; } writeByte((byte)i); } /** Writes a long as eight bytes. *

* 64-bit unsigned integer written as eight bytes, high-order bytes first. * * @see DataInput#readLong() */ public void writeLong(long i) throws IOException { writeInt((int) (i >> 32)); writeInt((int) i); } /** Writes an long in a variable-length format. Writes between one and nine * bytes. Smaller values take fewer bytes. Negative numbers are not * supported. *

* The format is described further in {@link DataOutput#writeVInt(int)}. * @see DataInput#readVLong() */ public final void writeVLong(long i) throws IOException { assert i >= 0L; while ((i & ~0x7FL) != 0L) { writeByte((byte)((i & 0x7FL) | 0x80L)); i >>>= 7; } writeByte((byte)i); } /** Writes a string. *

* Writes strings as UTF-8 encoded bytes. First the length, in bytes, is * written as a {@link #writeVInt VInt}, followed by the bytes. * * @see DataInput#readString() */ public void writeString(String s) throws IOException { final BytesRef utf8Result = new BytesRef(10); UnicodeUtil.UTF16toUTF8(s, 0, s.length(), utf8Result); writeVInt(utf8Result.length); writeBytes(utf8Result.bytes, 0, utf8Result.length); } private static int COPY_BUFFER_SIZE = 16384; private byte[] copyBuffer; /** Copy numBytes bytes from input to ourself. */ public void copyBytes(DataInput input, long numBytes) throws IOException { assert numBytes >= 0: "numBytes=" + numBytes; long left = numBytes; if (copyBuffer == null) copyBuffer = new byte[COPY_BUFFER_SIZE]; while(left > 0) { final int toCopy; if (left > COPY_BUFFER_SIZE) toCopy = COPY_BUFFER_SIZE; else toCopy = (int) left; input.readBytes(copyBuffer, 0, toCopy); writeBytes(copyBuffer, 0, toCopy); left -= toCopy; } } /** * Writes a String map. *

* First the size is written as an {@link #writeInt(int) Int32}, * followed by each key-value pair written as two consecutive * {@link #writeString(String) String}s. * * @param map Input map. May be null (equivalent to an empty map) */ public void writeStringStringMap(Map map) throws IOException { if (map == null) { writeInt(0); } else { writeInt(map.size()); for(final Map.Entry entry: map.entrySet()) { writeString(entry.getKey()); writeString(entry.getValue()); } } } /** * Writes a String set. *

* First the size is written as an {@link #writeInt(int) Int32}, * followed by each value written as a * {@link #writeString(String) String}. * * @param set Input set. May be null (equivalent to an empty set) */ public void writeStringSet(Set set) throws IOException { if (set == null) { writeInt(0); } else { writeInt(set.size()); for(String value : set) { writeString(value); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy