Example usage for java.io BufferedInputStream reset

List of usage examples for java.io BufferedInputStream reset

Introduction

In this page you can find the example usage for java.io BufferedInputStream reset.

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

See the general contract of the reset method of InputStream.

Usage

From source file:org.apache.hadoop.tools.rumen.TestRumenJobTraces.java

@Test
public void testHadoop20JHParser() throws Exception {
    // Disabled// w ww  . jav  a  2  s  .  c om
    if (true)
        return;

    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);

    boolean success = false;

    final Path rootInputDir = new Path(System.getProperty("test.tools.input.dir", "")).makeQualified(lfs);
    final Path rootTempDir = new Path(System.getProperty("test.build.data", "/tmp")).makeQualified(lfs);

    final Path rootInputPath = new Path(rootInputDir, "rumen/small-trace-test");
    final Path tempDir = new Path(rootTempDir, "TestHadoop20JHParser");
    lfs.delete(tempDir, true);

    final Path inputPath = new Path(rootInputPath, "v20-single-input-log.gz");
    final Path goldPath = new Path(rootInputPath, "v20-single-input-log-event-classes.text.gz");

    InputStream inputLogStream = new PossiblyDecompressedInputStream(inputPath, conf);

    InputStream inputGoldStream = new PossiblyDecompressedInputStream(goldPath, conf);

    BufferedInputStream bis = new BufferedInputStream(inputLogStream);
    bis.mark(10000);
    Hadoop20JHParser parser = new Hadoop20JHParser(bis);

    final Path resultPath = new Path(tempDir, "result.text");

    System.out.println("testHadoop20JHParser sent its output to " + resultPath);

    Compressor compressor;

    FileSystem fs = resultPath.getFileSystem(conf);
    CompressionCodec codec = new CompressionCodecFactory(conf).getCodec(resultPath);
    OutputStream output;
    if (codec != null) {
        compressor = CodecPool.getCompressor(codec);
        output = codec.createOutputStream(fs.create(resultPath), compressor);
    } else {
        output = fs.create(resultPath);
    }

    PrintStream printStream = new PrintStream(output);

    try {
        assertEquals("Hadoop20JHParser can't parse the test file", true,
                Hadoop20JHParser.canParse(inputLogStream));

        bis.reset();

        HistoryEvent event = parser.nextEvent();

        while (event != null) {
            printStream.println(event.getClass().getCanonicalName());
            event = parser.nextEvent();
        }

        printStream.close();

        LineReader goldLines = new LineReader(inputGoldStream);
        LineReader resultLines = new LineReader(new PossiblyDecompressedInputStream(resultPath, conf));

        int lineNumber = 1;

        try {
            Text goldLine = new Text();
            Text resultLine = new Text();

            int goldRead = goldLines.readLine(goldLine);
            int resultRead = resultLines.readLine(resultLine);

            while (goldRead * resultRead != 0) {
                if (!goldLine.equals(resultLine)) {
                    assertEquals("Type mismatch detected", goldLine, resultLine);
                    break;
                }

                goldRead = goldLines.readLine(goldLine);
                resultRead = resultLines.readLine(resultLine);

                ++lineNumber;
            }

            if (goldRead != resultRead) {
                assertEquals("the " + (goldRead > resultRead ? "gold" : resultRead)
                        + " file contains more text at line " + lineNumber, goldRead, resultRead);
            }

            success = true;
        } finally {
            goldLines.close();
            resultLines.close();

            if (success) {
                lfs.delete(resultPath, false);
            }
        }

    } finally {
        if (parser == null) {
            inputLogStream.close();
        } else {
            if (parser != null) {
                parser.close();
            }
        }

        if (inputGoldStream != null) {
            inputGoldStream.close();
        }

        // it's okay to do this twice [if we get an error on input]
        printStream.close();
    }
}

From source file:org.apereo.portal.io.xml.JaxbPortalDataHandlerService.java

protected MediaType getMediaType(BufferedInputStream inputStream, String fileName) throws IOException {
    final TikaInputStream tikaInputStreamStream = TikaInputStream.get(new CloseShieldInputStream(inputStream));
    try {/*w ww.  j a v a 2s  .c  om*/
        final Detector detector = new DefaultDetector();
        final Metadata metadata = new Metadata();
        metadata.set(Metadata.RESOURCE_NAME_KEY, fileName);

        final MediaType type = detector.detect(tikaInputStreamStream, metadata);
        logger.debug("Determined '{}' for '{}'", type, fileName);
        return type;
    } catch (IOException e) {
        logger.warn("Failed to determine media type for '" + fileName + "' assuming XML", e);
        return null;
    } finally {
        IOUtils.closeQuietly(tikaInputStreamStream);

        //Reset the buffered stream to make up for anything read by the detector
        inputStream.reset();
    }
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.por.PORFileReader.java

private File decodeHeader(BufferedInputStream stream) throws IOException {
    File tempPORfile = null;/*from w ww  .j  a  va  2s .  c o m*/

    if (stream == null) {
        throw new IllegalArgumentException("file == null!");
    }

    byte[] headerByes = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(1000);
    }
    int nbytes = stream.read(headerByes, 0, POR_HEADER_SIZE);

    //printHexDump(headerByes, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException("decodeHeader: reading failure");
    } else if (nbytes < 491) {
        // Size test: by defnition, it must have at least
        // 491-byte header, i.e., the file size less than this threshold
        // is not a POR file
        dbgLog.fine("this file is NOT spss-por type");
        throw new IllegalArgumentException("file is not spss-por type");
    }
    // rewind the current reading position back to the beginning
    if (stream.markSupported()) {
        stream.reset();
    }

    // line-terminating characters are usually one or two by defnition
    // however, a POR file saved by a genuine SPSS for Windows
    // had a three-character line terminator, i.e., failed to remove the
    // original file's one-character terminator when it was opened, and
    // saved it with the default two-character terminator without
    // removing original terminators. So we have to expect such a rare
    // case
    //
    // terminator
    // windows [0D0A]=>   [1310] = [CR/LF]
    // unix    [0A]  =>   [10]
    // mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    //
    // terminating characters should be found at the following
    //                             column positions[counting from 0]:
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //           : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(headerByes);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }

    boolean windowsNewLine = true;
    if (three == nolines) {
        windowsNewLine = false; // lineTerminator = "0D0D0A"
    } else if ((ucase == nolines) && (wcase < nolines)) {
        windowsNewLine = false; // lineTerminator = "0A"
    } else if ((ucase < nolines) && (wcase == nolines)) {
        windowsNewLine = true; //lineTerminator = "0D0A"
    } else if ((mcase == nolines) && (wcase < nolines)) {
        windowsNewLine = false; //lineTerminator = "0D"
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    //dbgLog.fine("pormark =>" + pormarks + "<-");
    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("POR ID toke test: Passed");
        init();

        smd.getFileInformation().put("mimeType", MIME_TYPE);
        smd.getFileInformation().put("fileFormat", MIME_TYPE);

    } else {
        dbgLog.fine("this file is NOT spss-por type");
        throw new IllegalArgumentException("decodeHeader: POR ID token was not found");
    }

    // save the POR file without new line characters

    FileOutputStream fileOutPOR = null;
    Writer fileWriter = null;

    // Scanner class can handle three-character line-terminator
    Scanner porScanner = null;

    try {
        tempPORfile = File.createTempFile("tempPORfile.", ".por");
        fileOutPOR = new FileOutputStream(tempPORfile);
        fileWriter = new BufferedWriter(new OutputStreamWriter(fileOutPOR, "utf8"));
        porScanner = new Scanner(stream);

        // Because 64-bit and 32-bit machines decode POR's first 40-byte
        // sequence differently, the first 5 leader lines are skipped from
        // the new-line-stripped file

        int lineCounter = 0;
        while (porScanner.hasNextLine()) {
            lineCounter++;
            if (lineCounter <= 5) {
                String line = porScanner.nextLine().toString();
                dbgLog.fine("line=" + lineCounter + ":" + line.length() + ":" + line);
            } else {
                fileWriter.write(porScanner.nextLine().toString());
            }
        }
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (porScanner != null) {
            porScanner.close();
        }
    }

    return tempPORfile;
}

From source file:nl.minbzk.dwr.zoeken.enricher.util.XmlHtmlReader.java

/**
 * Returns the encoding declared in the <meta http-equiv="content-type" ..>, or NULL if none.
 * /*from   ww w  .j  av a  2  s  .  com*/
 * @param is
 * @param guessedEnc
 * @return String
 * @throws IOException
 */
private String getHtmlMeta(final BufferedInputStream is, final String guessedEnc) throws IOException {
    String encoding = null;

    if (guessedEnc != null) {
        // Mark the stream up until the maximum bytes we'd like to read (4096 * 2), which should be enough to account for double-byte encodings like UTF-16

        is.mark(BUFFER_SIZE * 2);

        // Rather than read until we run into the meta tag, we just read out 4096 * 4 bytes

        byte[] buffer = new byte[BUFFER_SIZE * 2];

        int count = is.read(buffer);

        if (count > 0) {
            String content = new String(buffer, 0, count, guessedEnc);

            if (content.indexOf("<") != -1 && content.indexOf(">") != -1) {
                Matcher contentTypeMatcher = META_CONTENT_TYPE_PATTERN.matcher(content);
                Matcher contentLanguageMatcher = META_CONTENT_LANGUAGE_PATTERN.matcher(content);

                // Either the first or second group contains the match, the format of should be equivalent to the Content-Type header

                if (contentTypeMatcher.find())
                    encoding = getContentTypeEncoding(
                            (StringUtils.isEmpty(contentTypeMatcher.group(1)) ? contentTypeMatcher.group(2)
                                    : contentTypeMatcher.group(1)));

                // Likewise for the language, except store it directly within the class

                if (contentLanguageMatcher.find()) {
                    String[] languages = (StringUtils.isEmpty(contentLanguageMatcher.group(1))
                            ? contentLanguageMatcher.group(2)
                            : contentLanguageMatcher.group(1)).split(",");

                    _languages = new ArrayList<String>();

                    for (String language : languages)
                        _languages.add(language.split("-")[0].trim());
                }
            }

            is.reset();
        }
    }

    return encoding;
}

From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java

/**
 * Returns the encoding declared in the <meta http-equiv="content-type" ..>, or NULL if none.
 * //w w  w . j  a v  a  2s .c o  m
 * @param is
 * @param guessedEnc
 * @return String
 * @throws IOException
 */
private String getHtmlMeta(final BufferedInputStream is, final String guessedEnc) throws IOException {
    String encoding = null;

    if (guessedEnc != null) {
        // Mark the stream up until the maximum bytes we'd like to read (4096 * 2), which should be enough to account for double-byte encodings like UTF-16

        is.mark(BUFFER_SIZE * 2);

        // Rather than read until we run into the meta tag, we just read out 4096 * 4 bytes

        byte[] buffer = new byte[BUFFER_SIZE * 2];

        int count = is.read(buffer);

        if (count > 0) {
            String content = new String(buffer, 0, count, guessedEnc);

            if (content.indexOf("<") != -1 && content.indexOf(">") != -1) {
                Matcher contentTypeMatcher = META_CONTENT_TYPE_PATTERN.matcher(content);
                Matcher contentLanguageMatcher = META_CONTENT_LANGUAGE_PATTERN.matcher(content);

                // Either the first or second group contains the match, the format of should be equivalent to the Content-Type header

                if (contentTypeMatcher.find())
                    encoding = getContentTypeEncoding(
                            (StringUtils.isEmpty(contentTypeMatcher.group(1)) ? contentTypeMatcher.group(2)
                                    : contentTypeMatcher.group(1)));

                // Likewise for the language, except store it directly within the class

                if (contentLanguageMatcher.find()) {
                    String languageList = StringUtils.isEmpty(contentLanguageMatcher.group(1))
                            ? contentLanguageMatcher.group(2)
                            : contentLanguageMatcher.group(1);

                    if (!StringUtils.isEmpty(languageList)) {
                        _languages = new ArrayList<String>();

                        for (String language : languageList.split(","))
                            _languages.add(language.trim());
                    }
                }
            }

            is.reset();
        }
    }

    return encoding;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReader.java

private File decodeHeader(BufferedInputStream stream) throws IOException {
    dbgLog.fine("decodeHeader(): start");
    File tempPORfile = null;/*  ww w  . j  a  v a2s .c  om*/

    if (stream == null) {
        throw new IllegalArgumentException("file == null!");
    }

    byte[] headerByes = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(1000);
    }
    int nbytes = stream.read(headerByes, 0, POR_HEADER_SIZE);

    //printHexDump(headerByes, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException("decodeHeader: reading failure");
    } else if (nbytes < 491) {
        // Size test: by defnition, it must have at least
        // 491-byte header, i.e., the file size less than this threshold
        // is not a POR file
        dbgLog.fine("this file is NOT spss-por type");
        throw new IllegalArgumentException("file is not spss-por type");
    }
    // rewind the current reading position back to the beginning
    if (stream.markSupported()) {
        stream.reset();
    }

    // line-terminating characters are usually one or two by defnition
    // however, a POR file saved by a genuine SPSS for Windows
    // had a three-character line terminator, i.e., failed to remove the
    // original file's one-character terminator when it was opened, and
    // saved it with the default two-character terminator without
    // removing original terminators. So we have to expect such a rare
    // case
    //
    // terminator
    // windows [0D0A]=>   [1310] = [CR/LF]
    // unix    [0A]  =>   [10]
    // mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    //
    // terminating characters should be found at the following
    //                             column positions[counting from 0]:
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //           : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(headerByes);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }

    boolean windowsNewLine = true;
    if (three == nolines) {
        windowsNewLine = false; // lineTerminator = "0D0D0A"
    } else if ((ucase == nolines) && (wcase < nolines)) {
        windowsNewLine = false; // lineTerminator = "0A"
    } else if ((ucase < nolines) && (wcase == nolines)) {
        windowsNewLine = true; //lineTerminator = "0D0A"
    } else if ((mcase == nolines) && (wcase < nolines)) {
        windowsNewLine = false; //lineTerminator = "0D"
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    //dbgLog.fine("pormark =>" + pormarks + "<-");
    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("POR ID toke test: Passed");
        init();

        dataTable.setOriginalFileFormat(MIME_TYPE);
        dataTable.setUnf("UNF:6:NOTCALCULATED");

    } else {
        dbgLog.fine("this file is NOT spss-por type");
        throw new IllegalArgumentException("decodeHeader: POR ID token was not found");
    }

    // save the POR file without new line characters

    FileOutputStream fileOutPOR = null;
    Writer fileWriter = null;

    // Scanner class can handle three-character line-terminator
    Scanner porScanner = null;

    try {
        tempPORfile = File.createTempFile("tempPORfile.", ".por");
        fileOutPOR = new FileOutputStream(tempPORfile);
        fileWriter = new BufferedWriter(new OutputStreamWriter(fileOutPOR, "utf8"));
        porScanner = new Scanner(stream);

        // Because 64-bit and 32-bit machines decode POR's first 40-byte
        // sequence differently, the first 5 leader lines are skipped from
        // the new-line-stripped file

        int lineCounter = 0;
        while (porScanner.hasNextLine()) {
            lineCounter++;
            if (lineCounter <= 5) {
                String line = porScanner.nextLine();
                dbgLog.fine("line=" + lineCounter + ":" + line.length() + ":" + line);
            } else {
                fileWriter.write(porScanner.nextLine());
            }
        }
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (porScanner != null) {
            porScanner.close();
        }
    }

    return tempPORfile;
}

From source file:org.jzkit.a2j.codec.runtime.ChunkingBERInputStream.java

public int completeConstructedType(BufferedInputStream in, ByteArrayOutputStream baos, int level)
        throws java.io.IOException {

    int bytes_written = 0;

    int first_byte = in.read();
    if (first_byte == -1)
        throw new java.io.IOException("EOF");
    byte c = (byte) first_byte;
    // byte c = (byte)in.read();
    baos.write(c);/*from  w  ww . j  a va2s  .c  om*/
    bytes_written++;

    c &= 0xFF;
    int next_tag_class = c & 0xC0;
    boolean next_is_constructed = (c & 0x20) != 0;
    boolean next_is_indefinite = false;

    int next_tag_number = c & 0x1F;

    // If there are multiple octets to encode the tag
    if (next_tag_number == 0x1F) {
        next_tag_number = 0;
        do {
            c = (byte) in.read();
            baos.write(c);
            bytes_written++;

            // Shift value 7 bits left
            next_tag_number = next_tag_number << 7;

            // Merge with the octets we just got
            next_tag_number = (next_tag_number | (c & 0x7F));
        } while ((c & 0x80) != 0);
    }

    // dbg("tag: "+next_tag_number+" class:"+next_tag_class, level);

    int datalen;
    byte lenpart = (byte) in.read();
    baos.write(lenpart);
    bytes_written++;

    // System.err.println("First len octet is "+lenpart);
    if ((lenpart & 0x80) == 0) // If bit 8 is 0
    {
        // Single octet length encoding
        // System.err.println("Single octet length encoding");
        datalen = lenpart;
        next_is_indefinite = false;
    } else if ((lenpart & 0x7F) == 0) // Otherwise we are multiple octets (Maybe 0, which = indefinite)
    {
        // System.err.println("Indefinite length encoding");
        next_is_indefinite = true;
        datalen = 0;
    } else {
        next_is_indefinite = false;
        // System.err.println("Multiple octet length encoding ("+(lenpart & 0x7F )+"octets)");
        lenpart &= 0x7F;

        datalen = 0;
        while (lenpart-- > 0) {
            byte lenbyte = (byte) in.read();
            datalen = (datalen << 8) | (lenbyte & 0xFF);
            baos.write(lenbyte);
            bytes_written++;
        }
    }

    // System.err.print(" indefinite: "+next_is_indefinite+" cons:"+next_is_constructed+" len:"+datalen);

    // OK, len now contains the size of the octets.
    // If it's definite length encoding, just copy that many bytes
    if (next_is_indefinite) {
        if (next_is_constructed) {
            // System.err.print(" {\n");
            // Peek ahead looking for terminating octets.
            boolean more_data = true;
            in.mark(5);
            byte i1 = (byte) in.read();
            byte i2 = (byte) in.read();
            in.reset();
            if ((i1 == 0) && (i2 == 0)) {
                more_data = false;
            }

            while (more_data) {
                completeConstructedType(in, baos, level + 1);
                in.mark(5);
                i1 = (byte) in.read();
                i2 = (byte) in.read();
                in.reset();
                if ((i1 == 0) && (i2 == 0)) {
                    more_data = false;
                }
            }

            // Better consume the terminating octets.
            in.read();
            in.read();

            baos.write(0);
            bytes_written++;
            baos.write(0);
            bytes_written++;
            // dbg("} ("+bytes_written+")\n",level);
        } else {
            // Indefinite length primitive type
            // System.err.print(" Indefinite length primitive type");
            byte b1 = (byte) in.read();
            baos.write(b1);
            bytes_written++;
            byte b2 = (byte) in.read();
            baos.write(b2);
            bytes_written++;
            while (!((b1 == 0) && (b2 == 0))) {
                b1 = b2;
                b2 = (byte) in.read();
                baos.write(b2);
                bytes_written++;
            }
            // System.err.println("("+bytes_written+")");
        }
    } else {
        // System.err.println("copy definite length encoding, remain="+datalen);
        if (next_is_constructed) {
            // System.err.println(" {");
            while (datalen > 0) {
                int child_len = completeConstructedType(in, baos, level + 1);
                datalen -= child_len;
                bytes_written += child_len;
            }
            // dbg("} ("+bytes_written+")\n",level);
        } else {
            // System.err.print(" Definite length primitive type");
            byte[] buff = new byte[4096];
            while (datalen > 0) {
                int bytes_read = in.read(buff, 0, datalen > 4096 ? 4096 : datalen);
                // System.err.println("processed "+bytes_read);
                baos.write(buff, 0, bytes_read);
                datalen -= bytes_read;
                bytes_written += bytes_read;
                // System.err.println("("+bytes_written+")");
            }
        }
    }

    return bytes_written;
}

From source file:org.lockss.remote.RemoteApi.java

public BatchAuStatus processSavedConfigProps(BufferedInputStream auTxtStream)
        throws IOException, InvalidAuConfigBackupFile {
    int commentLen = AU_BACKUP_FILE_COMMENT.length();
    // There is apparently hidden buffering in the InputStreamReader's
    // StreamDecoder which throws off our calculation as to how much
    // auTxtStream needs to buffer, so use a large number
    auTxtStream.mark(paramBackupStreamMarkSize);
    BufferedReader rdr = new BufferedReader(new InputStreamReader(auTxtStream, Constants.DEFAULT_ENCODING),
            commentLen * 2);/*from  www  .j av a  2  s .c o  m*/
    // We really want rdr.readLine(), but we need to limit amount it reads
    // (in case it doesn't find newline)
    char[] buf = new char[commentLen];
    int chars = StreamUtil.readChars(rdr, buf, commentLen);
    log.debug3("chars: " + chars);
    if (chars == 0) {
        throw new InvalidAuConfigBackupFile("Uploaded file is empty");
    }
    String line1 = new String(buf);
    if (chars < commentLen || !line1.startsWith(AU_BACKUP_FILE_COMMENT)) {
        log.debug("line1: " + line1);
        throw new InvalidAuConfigBackupFile("Uploaded file does not appear to be a saved AU configuration");
    }
    try {
        auTxtStream.reset();
    } catch (IOException e) {
        throw new IOException("Internal error: please report \"Insufficient buffering for restore\".");
    }
    Properties allAuProps = new Properties();
    try {
        allAuProps.load(auTxtStream);
    } catch (Exception e) {
        log.warning("Loading AU config backup file", e);
        throw new InvalidAuConfigBackupFile("Uploaded file has illegal format: " + e.getMessage());
    }
    Configuration allAuConfig = ConfigManager.fromPropertiesUnsealed(allAuProps);
    int ver = checkLegalAuConfigTree(allAuConfig);
    return batchProcessAus(false, BATCH_ADD_RESTORE, allAuConfig, null);
}

From source file:org.sakaiproject.gradebook.gwt.server.ImportExportUtilityImpl.java

public Upload parseImportXLS(MultipartFile file, ImportSettings settings)
        throws InvalidInputException, FatalException, IOException {
    log.debug("parseImportXLS() called");

    // Strip off extension
    String fileName = removeFileExenstion(file.getOriginalFilename().toLowerCase());

    String realFileName = fileName;
    boolean isOriginalName;

    try {//from   ww  w .  ja  va2  s .  c  o m
        realFileName = getUniqueItemName(fileName, settings.getGradebookUid());
    } catch (GradebookImportException e) {
        return emptyUploadFileWithNotes(e.getMessage());
    }
    isOriginalName = realFileName.equals(fileName);

    log.debug("realFileName=" + realFileName);
    log.debug("isOriginalName=" + isOriginalName);

    org.apache.poi.ss.usermodel.Workbook inspread = null;

    BufferedInputStream bufStream = new BufferedInputStream(file.getInputStream());

    //inspread = readPoiSpreadsheet(tempfi);
    ///TODO: this is broken buonly called by the *Test class for this class and not by default

    if (inspread != null) {
        log.debug("Found a POI readable spreadsheet");
        bufStream.close();
        return handlePoiSpreadSheet(inspread, realFileName, isOriginalName, settings);
    }
    // else

    log.debug("POI couldn't handle the spreadsheet, using jexcelapi");
    bufStream.reset();
    return handleJExcelAPISpreadSheet(bufStream, realFileName, isOriginalName, settings);

}

From source file:tufts.vue.URLResource.java

private Properties scrapeHTMLmetaData(URLConnection connection, int maxSearchBytes) throws java.io.IOException {
    Properties metaData = new Properties();

    InputStream byteStream = connection.getInputStream();

    if (DEBUG.DND && DEBUG.META) {
        System.err.println("Getting headers from " + connection);
        System.err.println("Headers: " + connection.getHeaderFields());
    }//from   w  ww .  j  a va2  s .  co  m

    // note: be sure to call getContentType and don't rely on getting it from the HeaderFields map,
    // as sometimes it's set by the OS for a file:/// URL when there are no header fields (no http server)
    // (actually, this is set by java via a mime type table based on file extension, or a guess based on the stream)
    if (DEBUG.DND)
        System.err.println("*** getting contentType & encoding...");
    final String contentType = connection.getContentType();
    final String contentEncoding = connection.getContentEncoding();
    final int contentLength = connection.getContentLength();

    if (DEBUG.DND)
        System.err.println("*** contentType [" + contentType + "]");
    if (DEBUG.DND)
        System.err.println("*** contentEncoding [" + contentEncoding + "]");
    if (DEBUG.DND)
        System.err.println("*** contentLength [" + contentLength + "]");

    setProperty("url.contentType", contentType);
    setProperty("url.contentEncoding", contentEncoding);
    if (contentLength >= 0)
        setProperty("url.contentLength", contentLength);

    //if (contentType.toLowerCase().startsWith("text/html") == false) {
    if (!isHTML()) { // we only currently handle HTML
        if (DEBUG.Enabled)
            System.err.println("*** contentType [" + contentType + "] not HTML; skipping title extraction");
        return metaData;
    }

    if (DEBUG.DND)
        System.err.println("*** scanning for HTML meta-data...");

    try {
        final BufferedInputStream bufStream = new BufferedInputStream(byteStream, maxSearchBytes);
        bufStream.mark(maxSearchBytes);

        final byte[] byteBuffer = new byte[maxSearchBytes];
        int bytesRead = 0;
        int len = 0;
        // BufferedInputStream still won't read thru a block, so we need to allow
        // a few reads here to get thru a couple of blocks, so we can get up to
        // our maxbytes (e.g., a common return chunk count is 1448 bytes, presumably related to the MTU)
        do {
            int max = maxSearchBytes - bytesRead;
            len = bufStream.read(byteBuffer, bytesRead, max);
            System.out.println("*** read " + len);
            if (len > 0)
                bytesRead += len;
            else if (len < 0)
                break;
        } while (len > 0 && bytesRead < maxSearchBytes);
        if (DEBUG.DND)
            System.out.println("*** Got total chars: " + bytesRead);
        String html = new String(byteBuffer, 0, bytesRead);
        if (DEBUG.DND && DEBUG.META)
            System.out.println("*** HTML-STRING[" + html + "]");

        // first, look for a content encoding, so we can search for and get the title
        // on a properly encoded character stream

        String charset = null;

        Matcher cm = Content_Charset_Regex.matcher(html);
        if (cm.lookingAt()) {
            charset = cm.group(1);
            if (DEBUG.DND)
                System.err.println("*** found HTML specified charset [" + charset + "]");
            setProperty("charset", charset);
        }

        if (charset == null && contentEncoding != null) {
            if (DEBUG.DND || true)
                System.err.println("*** no charset found: using contentEncoding charset " + contentEncoding);
            charset = contentEncoding;
        }

        final String decodedHTML;

        if (charset != null) {
            bufStream.reset();
            InputStreamReader decodedStream = new InputStreamReader(bufStream, charset);
            //InputStreamReader decodedStream = new InputStreamReader(new ByteArrayInputStream(byteBuffer), charset);
            if (true || DEBUG.DND)
                System.out.println("*** decoding bytes into characters with official encoding "
                        + decodedStream.getEncoding());
            setProperty("contentEncoding", decodedStream.getEncoding());
            char[] decoded = new char[bytesRead];
            int decodedChars = decodedStream.read(decoded);
            decodedStream.close();
            if (true || DEBUG.DND)
                System.err.println("*** " + decodedChars + " characters decoded using " + charset);
            decodedHTML = new String(decoded, 0, decodedChars);
        } else
            decodedHTML = html; // we'll just have to go with the default platform charset...

        // these needed to be left open till the decodedStream was done, which
        // although it should never need to read beyond what's already buffered,
        // some internal java code has checks that make sure the underlying stream
        // isn't closed, even it it isn't used.
        byteStream.close();
        bufStream.close();

        Matcher m = HTML_Title_Regex.matcher(decodedHTML);
        if (m.lookingAt()) {
            String title = m.group(1);
            if (true || DEBUG.DND)
                System.err.println("*** found title [" + title + "]");
            metaData.put("title", title.trim());
        }

    } catch (Throwable e) {
        System.err.println("scrapeHTMLmetaData: " + e);
        if (DEBUG.DND)
            e.printStackTrace();
    }

    if (DEBUG.DND || DEBUG.Enabled)
        System.err.println("*** scrapeHTMLmetaData returning [" + metaData + "]");
    return metaData;
}