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

org.apache.lucene.codecs.CodecUtil 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.codecs;

/*
 * 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 org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.BytesRef;

/**
 * Utility class for reading and writing versioned headers.
 * 

* Writing codec headers is useful to ensure that a file is in * the format you think it is. * * @lucene.experimental */ public final class CodecUtil { private CodecUtil() {} // no instance /** * Constant to identify the start of a codec header. */ public final static int CODEC_MAGIC = 0x3fd76c17; /** * Writes a codec header, which records both a string to * identify the file and a version number. This header can * be parsed and validated with * {@link #checkHeader(DataInput, String, int, int) checkHeader()}. *

* CodecHeader --> Magic,CodecName,Version *

    *
  • Magic --> {@link DataOutput#writeInt Uint32}. This * identifies the start of the header. It is always {@value #CODEC_MAGIC}. *
  • CodecName --> {@link DataOutput#writeString String}. This * is a string to identify this file. *
  • Version --> {@link DataOutput#writeInt Uint32}. Records * the version of the file. *
*

* Note that the length of a codec header depends only upon the * name of the codec, so this length can be computed at any time * with {@link #headerLength(String)}. * * @param out Output stream * @param codec String to identify this file. It should be simple ASCII, * less than 128 characters in length. * @param version Version number * @throws IOException If there is an I/O error writing to the underlying medium. */ public static void writeHeader(DataOutput out, String codec, int version) throws IOException { BytesRef bytes = new BytesRef(codec); if (bytes.length != codec.length() || bytes.length >= 128) { throw new IllegalArgumentException("codec must be simple ASCII, less than 128 characters in length [got " + codec + "]"); } out.writeInt(CODEC_MAGIC); out.writeString(codec); out.writeInt(version); } /** * Computes the length of a codec header. * * @param codec Codec name. * @return length of the entire codec header. * @see #writeHeader(DataOutput, String, int) */ public static int headerLength(String codec) { return 9+codec.length(); } /** * Reads and validates a header previously written with * {@link #writeHeader(DataOutput, String, int)}. *

* When reading a file, supply the expected codec and * an expected version range (minVersion to maxVersion). * * @param in Input stream, positioned at the point where the * header was previously written. Typically this is located * at the beginning of the file. * @param codec The expected codec name. * @param minVersion The minimum supported expected version number. * @param maxVersion The maximum supported expected version number. * @return The actual version found, when a valid header is found * that matches codec, with an actual version * where minVersion <= actual <= maxVersion. * Otherwise an exception is thrown. * @throws CorruptIndexException If the first four bytes are not * {@link #CODEC_MAGIC}, or if the actual codec found is * not codec. * @throws IndexFormatTooOldException If the actual version is less * than minVersion. * @throws IndexFormatTooNewException If the actual version is greater * than maxVersion. * @throws IOException If there is an I/O error reading from the underlying medium. * @see #writeHeader(DataOutput, String, int) */ public static int checkHeader(DataInput in, String codec, int minVersion, int maxVersion) throws IOException { // Safety to guard against reading a bogus string: final int actualHeader = in.readInt(); if (actualHeader != CODEC_MAGIC) { throw new CorruptIndexException("codec header mismatch: actual header=" + actualHeader + " vs expected header=" + CODEC_MAGIC + " (resource: " + in + ")"); } return checkHeaderNoMagic(in, codec, minVersion, maxVersion); } /** Like {@link * #checkHeader(DataInput,String,int,int)} except this * version assumes the first int has already been read * and validated from the input. */ public static int checkHeaderNoMagic(DataInput in, String codec, int minVersion, int maxVersion) throws IOException { final String actualCodec = in.readString(); if (!actualCodec.equals(codec)) { throw new CorruptIndexException("codec mismatch: actual codec=" + actualCodec + " vs expected codec=" + codec + " (resource: " + in + ")"); } final int actualVersion = in.readInt(); if (actualVersion < minVersion) { throw new IndexFormatTooOldException(in, actualVersion, minVersion, maxVersion); } if (actualVersion > maxVersion) { throw new IndexFormatTooNewException(in, actualVersion, minVersion, maxVersion); } return actualVersion; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy