1384N/A/*
1384N/A * CDDL HEADER START
1384N/A *
1384N/A * The contents of this file are subject to the terms of the
1384N/A * Common Development and Distribution License (the "License").
1384N/A * You may not use this file except in compliance with the License.
1384N/A *
1384N/A * See LICENSE.txt included in this distribution for the specific
1384N/A * language governing permissions and limitations under the License.
1384N/A *
1384N/A * When distributing Covered Code, include this CDDL HEADER in each
1384N/A * file and include the License file at LICENSE.txt.
1384N/A * If applicable, add the following below this CDDL HEADER, with the
1384N/A * fields enclosed by brackets "[]" replaced with your own identifying
1384N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1384N/A *
1384N/A * CDDL HEADER END
1384N/A */
1384N/A
1384N/A/*
1384N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1384N/A * Portions Copyright 2012 Jens Elkner.
1384N/A */
1384N/Apackage org.opensolaris.opengrok.analysis;
1384N/A
1384N/Aimport java.io.BufferedInputStream;
1384N/Aimport java.io.File;
1384N/Aimport java.io.FileInputStream;
1384N/Aimport java.io.FilterInputStream;
1384N/Aimport java.io.IOException;
1384N/Aimport java.io.InputStreamReader;
1384N/Aimport java.io.OutputStream;
1384N/Aimport java.io.Writer;
1384N/Aimport java.util.logging.Level;
1384N/Aimport java.util.logging.Logger;
1384N/Aimport java.util.zip.GZIPInputStream;
1384N/A
1384N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzer.Genre;
1384N/Aimport org.opensolaris.opengrok.util.IOUtils;
1384N/A
1384N/A/**
1384N/A * A {@link FilterInputStream} for Opengrok crossfiles.
1384N/A * It uses a {@link BufferedInputStream} or {@link GZIPInputStream} as
1385N/A * underlying stream to provide the required functionality. So there is usually
1385N/A * no need to wrap it into another BufferedInputStream etc. .
1384N/A *
1384N/A * @see BufferedInputStream
1384N/A * @see GZIPInputStream
1384N/A *
1384N/A * @author Jens Elkner
1384N/A * @version $Revision$
1384N/A */
1384N/Apublic class XrefInputStream extends FilterInputStream {
1384N/A private static final Logger logger = Logger.getLogger(XrefInputStream.class.getName());
1384N/A private XrefHeader header;
1384N/A private File file;
1384N/A private boolean uncompress;
1384N/A private boolean canClose;
1384N/A
1384N/A /**
1384N/A * Create a new InputStream from the given crossfile. The header gets read
1384N/A * automatically and thus internal cursor points to the start of the data
1384N/A * section of the crossfile, when this method returns.
1384N/A *
1384N/A * @param file file to read.
1384N/A * @param uncompress if {@code true} crossfile data gets uncompressed on
1384N/A * the fly if they are compressed.
1384N/A * @throws IOException on read error or if the given file is not a crossfile
1384N/A */
1384N/A public XrefInputStream(File file, boolean uncompress) throws IOException {
1384N/A super(null);
1384N/A FileInputStream fis = new FileInputStream(file);
1384N/A this.file = file;
1384N/A try {
1384N/A header = new XrefHeader(fis);
1384N/A this.uncompress = uncompress && header.isCompressed();
1384N/A in = this.uncompress
1384N/A ? new GZIPInputStream(fis, 4096)
1384N/A : new BufferedInputStream(fis, header.isCompressed() ? 4096 : 16384);
1384N/A } catch (IOException e) {
1384N/A if (in != null) {
1384N/A IOUtils.close(in);
1384N/A } else {
1384N/A IOUtils.close(fis);
1384N/A }
1384N/A throw new IOException(e.getMessage() + "(" + file + ")");
1384N/A }
1384N/A canClose = true;
1384N/A }
1384N/A
1384N/A /**
1384N/A * Get the header of the crossfile beeing read.
1384N/A * @return the crossfile header
1384N/A */
1384N/A public XrefHeader getHeader() {
1384N/A return header;
1384N/A }
1384N/A
1384N/A /**
1384N/A * Convinience method to check, whether the wrapped crossfile contains
1384N/A * compressed data.
1384N/A * @return {@code true} if data section is compressed.
1384N/A * @see XrefHeader#isCompressed()
1384N/A * @see #getHeader()
1384N/A */
1384N/A public boolean isCompressed() {
1384N/A return header.isCompressed();
1384N/A }
1384N/A
1384N/A /**
1384N/A * Convinience method to get the Genre of data provided by this stream.
1384N/A * @return the data's genre.
1384N/A * @see XrefHeader#getGenre()
1384N/A * @see #getHeader()
1384N/A */
1384N/A public Genre getGenre() {
1384N/A return header.getGenre();
1384N/A }
1384N/A
1384N/A /**
1384N/A * Get the file from which this stream has been created.
1384N/A * @return the origin of this stream
1384N/A */
1384N/A public File getFile() {
1384N/A return file;
1384N/A }
1384N/A
1384N/A /**
1384N/A * Dump all data not yet read to the given output stream.
1384N/A * @param out where to dump remaining data.
1384N/A * @throws IOException
1384N/A */
1384N/A public void dump(OutputStream out) throws IOException {
1384N/A if (out == null) {
1384N/A throw new IllegalArgumentException("null output stream not allowed");
1384N/A }
1384N/A byte[] buf =
1384N/A new byte[!uncompress && (in instanceof GZIPInputStream) ? 4096 : 16384];
1384N/A int len = 0;
1384N/A while ((len = in.read(buf)) >= 0) {
1384N/A out.write(buf, 0, len);
1384N/A }
1384N/A }
1384N/A
1384N/A /**
1384N/A * Dump all data not yet read to the given writer. The constructor for this
1384N/A * instance should have been called with on-the-fly uncompression enabled.
1384N/A * If not and the data of the stream are compressed, it is tried to switch
1384N/A * over to on-the-fly uncompression, but obviously this will fail with an
1384N/A * IOException, if any data have been read from this stream already.
1384N/A *
1384N/A * @param out where to write data.
1384N/A * @throws IOException on read error
1384N/A * @see XrefInputStream#XrefInputStream(File, boolean)
1384N/A */
1384N/A public void dump(Writer out) throws IOException {
1384N/A if (out == null) {
1384N/A throw new IllegalArgumentException("null output stream not allowed");
1384N/A }
1384N/A if (header.isCompressed() && !uncompress) {
1384N/A logger.warning("switching over to on-the-fly uncompression for "
1384N/A + file + " (wasn't specified in constructor)");
1384N/A GZIPInputStream gin = new GZIPInputStream(in, 4096);
1384N/A in = gin;
1384N/A }
1384N/A InputStreamReader isr = new InputStreamReader(in, "UTF-8");
1384N/A char[] buf = new char[4096]; // underlying buffers are suffient
1384N/A int len = 0;
1384N/A canClose = false; // avoid that isr.close() closes in as well
1384N/A try {
1384N/A while ((len = isr.read(buf)) >= 0) {
1384N/A out.write(buf, 0, len);
1384N/A }
1384N/A } finally {
1384N/A IOUtils.close(isr);
1384N/A canClose = true;
1384N/A }
1384N/A }
1384N/A
1384N/A /**
1384N/A * Convinience method to dump a crossfile to the given writer..
1384N/A *
1384N/A * @param file crossfile to dump.
1384N/A * @param out dump destination.
1384N/A * @param script if {@code true} the javascript snippet
1384N/A * {@code O.lines=$num; O.createLinenums();} gets emitted to the writer
1384N/A * first.
1384N/A * @return {@code true} on success.
1384N/A */
1384N/A public static boolean dump(File file, Writer out, boolean script) {
1384N/A if (out == null) {
1384N/A return false;
1384N/A }
1384N/A XrefInputStream in = null;
1384N/A try {
1384N/A in = new XrefInputStream(file, true);
1384N/A if (script) {
1384N/A out.write("<script type='text/javascript'>/* <![CDATA[ */O.lines=");
1384N/A out.write(Integer.toString(in.getHeader().getLines()));
1384N/A out.write(";O.createLinenums();/* ]]> */</script>");
1384N/A }
1384N/A in.dump(out);
1384N/A return true;
1384N/A } catch (Exception e) {
1384N/A logger.warning(e.getLocalizedMessage());
1384N/A logger.log(Level.FINE, "dump " + file, e);
1384N/A } finally {
1384N/A IOUtils.close(in);
1384N/A }
1384N/A return false;
1384N/A }
1384N/A
1384N/A /**
1384N/A * {@inheritDoc}
1384N/A */
1384N/A @Override
1384N/A public void close() throws IOException {
1384N/A if (!canClose) {
1384N/A return;
1384N/A }
1384N/A in.close();
1384N/A }
1384N/A
1384N/A /**
1384N/A * Dump an opengrok cross file.
1384N/A * @param args opengrok crossfile name
1384N/A */
1384N/A public static void main(String[] args) {
1384N/A if (args.length == 0) {
1384N/A System.out.println("Usage: java ... " + logger.getName() + " xrefFile");
1384N/A return;
1384N/A }
1384N/A XrefInputStream in = null;
1384N/A try {
1384N/A in = new XrefInputStream(new File(args[0]), true);
1384N/A System.out.println(in.getHeader().toString());
1384N/A System.out.println();
1384N/A in.dump(System.out);
1384N/A } catch (IOException e) {
1384N/A System.out.println(e.getLocalizedMessage());
1384N/A } finally {
1384N/A IOUtils.close(in);
1384N/A }
1384N/A }
1384N/A}