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