org.apache.maven.index.reader.ChunkWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of indexer-reader Show documentation
Show all versions of indexer-reader Show documentation
Indexer Reader is a dependency-less library that is able to read published (remote) index with incremental update
support, making usable to integrate published Maven Indexes into any engine without depending on
maven-indexer-core and its transitive dependencies.
package org.apache.maven.index.reader;
/*
* 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.Closeable;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
/**
* Maven Index published binary chunk writer, it writes raw Maven Indexer records to the transport binary format.
* Instances of this class MUST BE handled as resources (have them closed once done with them), it is user
* responsibility to close them, ideally in try-with-resource block.
*
* @since 5.1.2
*/
public class ChunkWriter
implements Closeable
{
private static final int F_INDEXED = 1;
private static final int F_TOKENIZED = 2;
private static final int F_STORED = 4;
private final String chunkName;
private final DataOutputStream dataOutputStream;
private final int version;
private final Date timestamp;
public ChunkWriter( final String chunkName,
final OutputStream outputStream,
final int version,
final Date timestamp )
throws IOException
{
this.chunkName = chunkName.trim();
this.dataOutputStream = new DataOutputStream( new GZIPOutputStream( outputStream, 2 * 1024 ) );
this.version = version;
this.timestamp = timestamp;
dataOutputStream.writeByte( version );
dataOutputStream.writeLong( timestamp == null ? -1 : timestamp.getTime() );
}
/**
* Returns the chunk name.
*/
public String getName()
{
return chunkName;
}
/**
* Returns index version. All releases so far always returned {@code 1}.
*/
public int getVersion()
{
return version;
}
/**
* Returns the index timestamp of last update of the index.
*/
public Date getTimestamp()
{
return timestamp;
}
/**
* Writes out the record iterator and returns the written record count.
*/
public int writeChunk( final Iterator
© 2015 - 2024 Weber Informatics LLC | Privacy Policy