home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (CodecUtil.java)

This example Lucene source code file (CodecUtil.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

ascii, bytesref, codec_magic, codec_magic, codecutil, corruptindexexception, corruptindexexception, illegalargumentexception, indexformattoonewexception, indexformattoooldexception, io, ioexception, ioexception, string, string

The Lucene CodecUtil.java source code

package org.apache.lucene.util;

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException;

import java.io.IOException;

 * @lucene.experimental

public final class CodecUtil {
  private CodecUtil() {} // no instance

  private final static int CODEC_MAGIC = 0x3fd76c17;

  public static DataOutput 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 + "]");

    return out;

  public static int headerLength(String codec) {
    return 9+codec.length();

  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);

    final String actualCodec = in.readString();
    if (!actualCodec.equals(codec)) {
      throw new CorruptIndexException("codec mismatch: actual codec=" + actualCodec + " vs expected codec=" + codec);

    final int actualVersion = in.readInt();
    if (actualVersion < minVersion) {
      throw new IndexFormatTooOldException(null, actualVersion, minVersion, maxVersion);
    if (actualVersion > maxVersion) {
      throw new IndexFormatTooNewException(null, actualVersion, minVersion, maxVersion);

    return actualVersion;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene CodecUtil.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.