Example usage for java.io PushbackInputStream PushbackInputStream

List of usage examples for java.io PushbackInputStream PushbackInputStream

Introduction

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

Prototype

public PushbackInputStream(InputStream in, int size) 

Source Link

Document

Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use.

Usage

From source file:com.digitalpebble.behemoth.io.warc.HttpResponse.java

public HttpResponse(byte[] response) throws ProtocolException, IOException {

    PushbackInputStream in = // process response
            new PushbackInputStream(new ByteArrayInputStream(response), BUFFER_SIZE);

    StringBuilder line = new StringBuilder();

    boolean haveSeenNonContinueStatus = false;
    while (!haveSeenNonContinueStatus) {
        // parse status code line
        this.code = parseStatusLine(in, line);
        // parse headers
        parseHeaders(in, line);//  w  ww .ja  v a2s . c  om
        haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
    }

    readPlainContent(in);
}

From source file:eu.scape_project.up2ti.container.ArcContainer.java

@Override
public void init(String containerFileName, InputStream containerFileStream) throws IOException {
    this.containerFileName = containerFileName;
    // Read first two bytes to check if we have a gzipped input stream
    PushbackInputStream pb = new PushbackInputStream(containerFileStream, 2);
    byte[] signature = new byte[2];
    pb.read(signature);/*from   www  . j a  v  a2  s  .c  o  m*/
    pb.unread(signature);
    // use compressed reader if gzip magic number is matched
    if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) {
        reader = ArcReaderFactory.getReaderCompressed(pb);
    } else {
        reader = ArcReaderFactory.getReaderUncompressed(pb);
    }
    archiveRecords = new ArrayList<ArcRecordBase>();
    // initialise object by create temporary files and the bidirectional 
    // file-record map.
    arcRecContentsToTempFiles();
}

From source file:EOLConvertingInputStream.java

/**
 * Creates a new <code>EOLConvertingInputStream</code>
 * instance converting bytes in the given <code>InputStream</code>.
 * //www .j  a va2  s  .com
 * @param in the <code>InputStream</code> to read from.
 * @param flags one of <code>CONVERT_CR</code>, <code>CONVERT_LF</code> or
 *        <code>CONVERT_BOTH</code>.
 */
public EOLConvertingInputStream(InputStream in, int flags) {
    super();

    this.in = new PushbackInputStream(in, 2);
    this.flags = flags;
}

From source file:org.openhealthtools.openatna.syslog.GenericMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {/* w w  w .  j av  a2 s.  co m*/
        PushbackInputStream pin = new PushbackInputStream(in, 7);
        byte[] bytes = new byte[7];
        pin.read(bytes);

        boolean bsd = isBSD(bytes);
        pin.unread(bytes);
        if (bsd) {
            log.debug("message is BSD style");
            return bsdFactory.read(pin);
        } else {
            log.debug("message RFC 5424");
            return protFactory.read(pin);
        }
    } catch (IOException e) {
        throw new SyslogException(e);
    }
}

From source file:eu.scape_project.tpid.ContainerProcessing.java

/**
 * Prepare input//from w  w w .  j  ava  2 s. co m
 *
 * @param pt
 * @throws IOException IO Error
 * @throws java.lang.InterruptedException
 */
public void prepareInput(Path pt) throws InterruptedException, IOException {
    FileSystem fs = FileSystem.get(context.getConfiguration());
    InputStream containerFileStream = fs.open(pt);
    String containerFileName = pt.getName();

    ArcReader reader;
    // Read first two bytes to check if we have a gzipped input stream
    PushbackInputStream pb = new PushbackInputStream(containerFileStream, 2);
    byte[] signature = new byte[2];
    pb.read(signature);
    pb.unread(signature);
    // use compressed reader if gzip magic number is matched
    if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) {
        reader = ArcReaderFactory.getReaderCompressed(pb);
    } else {
        reader = ArcReaderFactory.getReaderUncompressed(pb);
    }
    long currTM = System.currentTimeMillis();
    String unpackHdfsPath = conf.get("unpack_hdfs_path", "tpid_unpacked");
    String hdfsUnpackDirStr = StringUtils.normdir(unpackHdfsPath, Long.toString(currTM));
    String hdfsJoboutputPath = conf.get("tooloutput_hdfs_path", "tpid_tooloutput");
    String hdfsOutputDirStr = StringUtils.normdir(hdfsJoboutputPath, Long.toString(currTM));

    Iterator<ArcRecordBase> arcIterator = reader.iterator();

    // Number of files which should be processed per invokation
    int numItemsPerInvocation = conf.getInt("num_items_per_task", 50);
    int numItemCounter = numItemsPerInvocation;
    // List of input files to be processed
    String inliststr = "";
    // List of output files to be generated
    String outliststr = "";
    try {
        while (arcIterator.hasNext()) {
            ArcRecordBase arcRecord = arcIterator.next();
            String recordKey = getRecordKey(arcRecord, containerFileName);
            String outFileName = RandomStringUtils.randomAlphabetic(25);
            String hdfsPathStr = hdfsUnpackDirStr + outFileName;
            Path hdfsPath = new Path(hdfsPathStr);
            String outputFileSuffix = conf.get("output_file_suffix", ".fits.xml");
            String hdfsOutPathStr = hdfsOutputDirStr + outFileName + outputFileSuffix;
            FSDataOutputStream hdfsOutStream = fs.create(hdfsPath);
            ArcUtils.recordToOutputStream(arcRecord, hdfsOutStream);
            Text key = new Text(recordKey);
            Text value = new Text(fs.getHomeDirectory() + File.separator + hdfsOutPathStr);
            mos.write("keyfilmapping", key, value);
            String scapePlatformInvoke = conf.get("scape_platform_invoke", "fits dirxml");
            Text ptmrkey = new Text(scapePlatformInvoke);
            // for the configured number of items per invokation, add the 
            // files to the input and output list of the command.
            inliststr += "," + fs.getHomeDirectory() + File.separator + hdfsPathStr;
            outliststr += "," + fs.getHomeDirectory() + File.separator + hdfsOutPathStr;
            if (numItemCounter > 1 && arcIterator.hasNext()) {
                numItemCounter--;
            } else if (numItemCounter == 1 || !arcIterator.hasNext()) {
                inliststr = inliststr.substring(1); // cut off leading comma 
                outliststr = outliststr.substring(1); // cut off leading comma 
                String pattern = conf.get("tomar_param_pattern", "%1$s %2$s");
                String ptMrStr = StringUtils.formatCommandOutput(pattern, inliststr, outliststr);
                Text ptmrvalue = new Text(ptMrStr);
                // emit tomar input line where the key is the tool invokation
                // (tool + operation) and the value is the parameter list
                // where input and output strings contain file lists.
                mos.write("tomarinput", ptmrkey, ptmrvalue);
                numItemCounter = numItemsPerInvocation;
                inliststr = "";
                outliststr = "";
            }
        }
    } catch (Exception ex) {
        mos.write("error", new Text("Error"), new Text(pt.toString()));
    }

}

From source file:QPDecoderStream.java

/**
 * Create a Quoted Printable decoder that decodes the specified 
 * input stream./*from   w  w  w  .  ja  v a2s .  c  o m*/
 * @param in        the input stream
 */
public QPDecoderStream(InputStream in) {
    super(new PushbackInputStream(in, 2)); // pushback of size=2
}

From source file:org.dspace.app.xmlui.cocoon.servlet.multipart.DSpaceMultipartParser.java

private void parseParts(int contentLength, String contentType, InputStream requestStream)
        throws IOException, MultipartException {
    this.contentLength = contentLength;
    if (contentLength > this.maxUploadSize) {
        this.oversized = true;
    }/* w  w  w. j av  a  2  s.c o m*/

    BufferedInputStream bufferedStream = new BufferedInputStream(requestStream);
    PushbackInputStream pushbackStream = new PushbackInputStream(bufferedStream, MAX_BOUNDARY_SIZE);
    DSpaceTokenStream stream = new DSpaceTokenStream(pushbackStream);

    parseMultiPart(stream, getBoundary(contentType));
}

From source file:mitm.common.util.AutoDetectUnicodeReader.java

/**
 * Conveniance constructor. If encoding is non-null, the encoding will not be detected but the given
 * encoding will be used. This constructor is added for conveniance to make it possible to use this
 * reader when the encoding is known.//  w w  w .ja v a  2  s . c  o  m
 */
public AutoDetectUnicodeReader(InputStream input, String encoding) {
    Check.notNull(input, "input");

    this.input = input;
    this.encoding = encoding;
    this.pushback = new PushbackInputStream(input, AUTO_DETECT_BYTES);
}

From source file:com.hs.mail.mailet.ToRepository.java

private void deliver(Collection<Recipient> recipients, SmtpMessage message) throws IOException {
    String returnPath = (message.getNode() != SmtpMessage.LOCAL)
            ? "Return-Path: <" + message.getFrom().getMailbox() + ">\r\n"
            : null;//from w w  w .  ja va2 s.c om
    MailMessage msg = message.getMailMessage();
    try {
        if (returnPath != null) {
            Header header = msg.getHeader().getHeader();
            header.setField(AbstractField.parse(returnPath));
            msg.setSize(msg.getSize() + returnPath.getBytes().length);
        }
        for (Recipient rcpt : recipients) {
            try {
                if (rcpt.getID() != -1) {
                    if (!Sieve.runSieve(context, rcpt, message)) {
                        context.storeMail(rcpt.getID(), ImapConstants.INBOX_NAME, message);
                    }
                }
            } catch (Exception e) {
                StringBuilder errorBuffer = new StringBuilder(256).append("Error while delivering message to ")
                        .append(rcpt);
                logger.error(errorBuffer.toString(), e);

                if (!message.isNotificationMessage()) {
                    errorBuffer.append(": ").append(e.getMessage().trim()).append("\r\n");
                    message.appendErrorMessage(errorBuffer.toString());
                }
            }
        }
    } catch (MimeException e) {
        // impossible really
    }
    if (msg != null && msg.getPhysMessageID() != 0) {
        try {
            if (returnPath != null) {
                PushbackInputStream is = new PushbackInputStream(msg.getInputStream(), returnPath.length());
                is.unread(returnPath.getBytes("ASCII"));
                msg.save(is);
            } else {
                msg.save(true);
            }
            builder.build(msg.getInternalDate(), msg.getPhysMessageID());
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (MimeException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

From source file:org.openhealthtools.openatna.syslog.bsd.BsdMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {//  w  w w  . j ava2 s  .  c  om
        PushbackInputStream pin = new PushbackInputStream(in, 5);
        int priority = readPriority(pin);
        int facility;
        int severity;
        byte c;
        int spaces = 4;
        int count = 0;
        boolean spaceBefore = false;
        ByteBuffer buff = ByteBuffer.wrap(new byte[256]);

        String timestamp;
        String month = null;
        String date = null;
        String time = null;
        String host = "";
        int max = 256;
        int curr = 0;

        while (count < spaces && curr < max) {
            c = (byte) pin.read();
            curr++;
            if (c == ' ') {
                if (!spaceBefore) {
                    count++;
                    String currHeader = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
                    buff.clear();
                    switch (count) {
                    case 1:
                        month = currHeader;
                        break;
                    case 2:
                        date = currHeader;
                        break;
                    case 3:
                        time = currHeader;
                        break;
                    case 4:
                        host = currHeader;
                        break;

                    }
                }
                spaceBefore = true;

            } else {
                spaceBefore = false;
                buff.put(c);
            }
        }
        if (month == null || date == null || time == null) {
            timestamp = createDate(new Date());
        } else {
            String gap = " ";
            if (date.length() == 1) {
                gap = "  ";
            }
            timestamp = (month + gap + date + " " + time);
            try {
                formatDate(timestamp);
            } catch (Exception e) {
                timestamp = createDate(new Date());
            }
        }
        String tag = null;
        int tagLen = 32;
        buff.clear();
        for (int i = 0; i < tagLen; i++) {
            c = (byte) pin.read();
            curr++;
            if (!Character.isLetterOrDigit((char) (c & 0xff))) {
                pin.unread(c);
                break;
            }
            buff.put(c);
        }
        if (buff.position() > 0) {
            tag = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
        }

        LogMessage logMessage = getLogMessage(tag);
        String encoding = readBom(pin, logMessage.getExpectedEncoding());
        logMessage.read(pin, encoding);
        facility = priority / 8;
        severity = priority % 8;
        return new BsdMessage(facility, severity, timestamp, host, logMessage, tag);
    } catch (IOException e) {
        e.printStackTrace();
        throw new SyslogException(e);
    }
}