Example usage for java.io BufferedInputStream mark

List of usage examples for java.io BufferedInputStream mark

Introduction

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

Prototype

public synchronized void mark(int readlimit) 

Source Link

Document

See the general contract of the mark method of InputStream.

Usage

From source file:com.baomidou.framework.upload.UploadMultipartRequest.java

/**
 * /*w  ww.  j a  va  2s. co  m*/
 */
private UploadFile writeTo(File fileOrDirectory, String fileName, FileRenamePolicy policy, FilePart filePart)
        throws IOException {
    OutputStream fileOut = null;
    UploadFile cf = new UploadFile();
    try {
        // Only do something if this part contains a file
        if (fileName != null) {
            BufferedInputStream in = new BufferedInputStream(filePart.getInputStream());
            cf.setType(filePart.getContentType());
            /**
             * ??
             * 
             * <p></p>
             */
            if (StringUtils.isNotBlank(fileHeaderExts)) {
                try {
                    /**
                     * ? 3 
                     */
                    byte[] data = new byte[3];
                    in.mark(3);
                    in.read(data, 0, data.length);
                    in.reset();
                    String fileExt = readFileExt(data, fileName);
                    if (fileExt != null) {
                        cf.setSuffix(fileExt);
                    } else {
                        cf.setUploadCode(UploadCode.ILLEGAL_EXT);
                        logger.debug(" upload fileType is null.");
                        return cf;
                    }
                } catch (Exception e) {
                    logger.debug("upload file error. ", e);
                    cf.setUploadCode(UploadCode.EXCEPTION);
                    return cf;
                }
            } else {
                cf.setSuffix(fileName.substring(fileName.lastIndexOf(".")));
            }

            // Check if user supplied directory
            File file;
            if (fileOrDirectory.isDirectory()) {
                // Write it to that dir the user supplied, 
                // with the filename it arrived with
                file = new File(fileOrDirectory, fileName);
            } else {
                // Write it to the file the user supplied,
                // ignoring the filename it arrived with
                file = fileOrDirectory;
            }

            if (policy instanceof UploadFileRenamePolicy) {
                ((UploadFileRenamePolicy) policy).setSuffix(cf.getSuffix());
            }

            if (policy != null) {
                file = policy.rename(file);
                fileName = file.getName();
                cf.setFilename(fileName);
            }

            fileOut = new BufferedOutputStream(new FileOutputStream(file));
            cf.setSize(write(fileOut, filePart.getContentType(), in));
        }
    } catch (Exception e) {
        logger.debug("upload file write error. ", e);
        cf.setUploadCode(UploadCode.EXCEPTION);
    } finally {
        if (fileOut != null)
            fileOut.close();
    }
    return cf;
}

From source file:com.hichinaschool.flashcards.libanki.importer.Anki2Importer.java

/**
 * Return the contents of the given input stream, limited to Anki2Importer.MEDIAPICKLIMIT bytes
 * This is only used for comparison of media files with the limited resources of mobile devices
 *//*from   w ww .  j  a  v  a 2  s  .c o  m*/
byte[] _mediaPick(BufferedInputStream is) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(MEDIAPICKLIMIT * 2);
        byte[] buf = new byte[MEDIAPICKLIMIT];
        int readLen;
        int readSoFar = 0;
        is.mark(MEDIAPICKLIMIT * 2);
        while (true) {
            readLen = is.read(buf);
            baos.write(buf);
            if (readLen == -1) {
                break;
            }
            readSoFar += readLen;
            if (readSoFar > MEDIAPICKLIMIT) {
                break;
            }
        }
        is.reset();
        byte[] result = new byte[MEDIAPICKLIMIT];
        System.arraycopy(baos.toByteArray(), 0, result, 0, Math.min(baos.size(), MEDIAPICKLIMIT));
        return result;
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:org.apache.storm.daemon.logviewer.handler.LogviewerLogSearchHandler.java

/**
 * Tries once to read ahead in the stream to fill the context and
 * resets the stream to its position before the call.
 *///from w w w .  jav a  2  s .  com
private byte[] tryReadAhead(BufferedInputStream stream, ByteBuffer haystack, int offset, int fileLength,
        int bytesRead) throws IOException {
    int numExpected = Math.min(fileLength - bytesRead, GREP_CONTEXT_SIZE);
    byte[] afterBytes = new byte[numExpected];
    stream.mark(numExpected);
    // Only try reading once.
    stream.read(afterBytes, 0, numExpected);
    stream.reset();
    return afterBytes;
}

From source file:org.sonatype.nexus.proxy.storage.local.fs.DefaultFSLocalRepositoryStorage.java

public void storeItem(Repository repository, StorageItem item)
        throws UnsupportedStorageOperationException, LocalStorageException {
    final File target;
    final ContentLocator originalContentLocator;
    if (item instanceof StorageFileItem) {
        originalContentLocator = ((StorageFileItem) item).getContentLocator();
    } else {//from w ww.j a  v a  2  s  .c om
        originalContentLocator = null;
    }
    try {
        // set some sanity stuff
        item.setStoredLocally(System.currentTimeMillis());
        item.setRemoteChecked(item.getStoredLocally());
        item.setExpired(false);

        ContentLocator cl = null;

        if (item instanceof StorageFileItem) {
            StorageFileItem fItem = (StorageFileItem) item;

            prepareStorageFileItemForStore(fItem);

            cl = fItem.getContentLocator();

            try {
                final BufferedInputStream bufferedInputStream;
                if (!cl.isReusable()) {
                    // link persister will close the stream, so prevent it doing so
                    bufferedInputStream = new BufferedInputStream(cl.getContent()) {
                        @Override
                        public void close() {
                            // nop
                        }
                    };
                    bufferedInputStream.mark(1024);
                    cl = new PreparedContentLocator(bufferedInputStream, cl.getMimeType(), cl.getLength());
                } else {
                    bufferedInputStream = null;
                }
                if (getLinkPersister().isLinkContent(cl)) {
                    // we are about to store a file that has link content, ban this store attempt
                    throw new UnsupportedStorageOperationException(
                            "Illegal Link API use on path " + item.getPath());
                }
                if (bufferedInputStream != null) {
                    bufferedInputStream.reset();
                }
            } catch (IOException e) {
                // meh, need to wrap it
                throw new LocalStorageException("Link-check failed ", e);
            }
        } else if (item instanceof StorageLinkItem) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            try {
                getLinkPersister().writeLinkContent((StorageLinkItem) item, bos);
            } catch (IOException e) {
                // should not happen, look at implementation
                // we will handle here two byte array backed streams!
                throw new LocalStorageException("Problem ", e);
            }

            cl = new ByteArrayContentLocator(bos.toByteArray(), "text/xml");
        }

        target = getFileFromBase(repository, item.getResourceStoreRequest());

        getFSPeer().storeItem(repository, getBaseDir(repository, item.getResourceStoreRequest()), item, target,
                cl);
    } finally {
        // NEXUS-5468: Ensure that in case of file item with prepared content
        // (typically those coming from RRS, as the content is actually wrapped HTTP response body, hence not reusable)
        // get closed irrelevant of the actual outcome. If all went right, stream was already closed,
        // and we will be "punished" by one extra (redundant) call to Closeable#close().
        if (originalContentLocator instanceof Closeable) {
            IOUtils.closeQuietly((Closeable) originalContentLocator);
        }
    }

    if (item instanceof StorageFileItem) {
        // replace content locator transparently, if we just consumed a non-reusable one
        // Hint: in general, those items coming from user uploads or remote proxy caching requests are non
        // reusable ones
        ((StorageFileItem) item)
                .setContentLocator(new FileContentLocator(target, ((StorageFileItem) item).getMimeType()));
    }

    final ContentLocator mdis = item instanceof StorageFileItem ? ((StorageFileItem) item).getContentLocator()
            : null;

    try {
        repository.getAttributesHandler().storeAttributes(item, mdis);
    } catch (IOException e) {
        throw new LocalStorageException("Cannot store attributes!", e);
    }
}

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   w w  w .j  a va2s. co 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[] 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.
 * /* ww  w.  j a  v a 2  s .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:org.apache.hadoop.tools.rumen.TestRumenJobTraces.java

@Test
public void testHadoop20JHParser() throws Exception {
    // Disabled//from   ww w.  ja  va  2 s . c o m
    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.rssowl.core.internal.connection.DefaultProtocolHandler.java

public String getLabel(URI link, IProgressMonitor monitor) throws ConnectionException {
    String title = ""; //$NON-NLS-1$

    /* Define Properties for Connection */
    Map<Object, Object> properties = new HashMap<Object, Object>();
    properties.put(IConnectionPropertyConstants.PROGRESS_MONITOR, monitor);
    properties.put(IConnectionPropertyConstants.CON_TIMEOUT, FEED_LABEL_CON_TIMEOUT);

    /* Open Stream */
    InputStream inS = openStream(link, properties);
    try {/*from  ww  w  . j  a  v  a2  s. c  o  m*/

        /* Return on Cancelation or Shutdown */
        if (monitor.isCanceled())
            return null;

        /* Buffered Stream to support mark and reset */
        BufferedInputStream bufIns = new BufferedInputStream(inS);
        bufIns.mark(8192);

        /* Try to read Encoding out of XML Document */
        String encoding = getEncodingFromXML(new InputStreamReader(bufIns), monitor);

        /* Avoid lowercase UTF-8 notation */
        if ("utf-8".equalsIgnoreCase(encoding)) //$NON-NLS-1$
            encoding = "UTF-8"; //$NON-NLS-1$

        /* Reset the Stream to its beginning */
        bufIns.reset();

        /* Grab Title using supplied Encoding */
        if (StringUtils.isSet(encoding) && Charset.isSupported(encoding))
            title = getTitleFromFeed(new BufferedReader(new InputStreamReader(bufIns, encoding)), monitor);

        /* Grab Title using Default Encoding */
        else
            title = getTitleFromFeed(new BufferedReader(new InputStreamReader(bufIns)), monitor);

        /* Remove the title tags (also delete attributes in title tag) */
        title = title.replaceAll("<title[^>]*>", ""); //$NON-NLS-1$ //$NON-NLS-2$
        title = title.replaceAll("</title>", ""); //$NON-NLS-1$ //$NON-NLS-2$

        /* Remove potential CDATA Tags */
        title = title.replaceAll(Pattern.quote("<![CDATA["), ""); //$NON-NLS-1$ //$NON-NLS-2$
        title = title.replaceAll(Pattern.quote("]]>"), ""); //$NON-NLS-1$ //$NON-NLS-2$
    } catch (IOException e) {
        if (!(e instanceof MonitorCanceledException))
            Activator.safeLogError(e.getMessage(), e);
    }

    /* Finally close the Stream */
    finally {
        closeStream(inS, true); //Abort the stream to avoid downloading the full content
    }

    // Have an upper maximum of title length to protect against issues
    String result = StringUtils.stripTags(title.trim(), true);
    if (result.length() > MAX_DETECTED_TITLE_LENGTH)
        result = result.substring(0, MAX_DETECTED_TITLE_LENGTH);

    return result;
}

From source file:org.sipfoundry.sipxconfig.phonebook.PhonebookManagerImpl.java

/**
 * Trying to determine if the file is vCard file
 *///from   www .j a  v  a2  s . com
boolean isVcard(BufferedInputStream is, String encoding) {
    final String vcardSignature = "BEGIN:VCARD";
    try {
        // keep buffer smaller than the readlimit: trying to ensure that we can reset the
        // stream
        BufferedReader isr = new BufferedReader(new InputStreamReader(is, encoding),
                vcardSignature.length() * 2);
        is.mark(vcardSignature.length() * 10);
        String line;
        do {
            line = isr.readLine();
        } while (StringUtils.isBlank(line));
        boolean isVcard = vcardSignature.equalsIgnoreCase(line);
        is.reset();
        return isVcard;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.parosproxy.paros.core.proxy.ProxyThread.java

@Override
public void run() {
    proxyThreadList.add(thread);//from   w  ww .ja  va  2  s. co  m
    boolean isSecure = this instanceof ProxyThreadSSL;
    HttpRequestHeader firstHeader = null;

    try {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inSocket.getInputStream(), 2048);
        inSocket = new CustomStreamsSocket(inSocket, bufferedInputStream, inSocket.getOutputStream());

        httpIn = new HttpInputStream(inSocket);
        httpOut = new HttpOutputStream(inSocket.getOutputStream());

        firstHeader = httpIn.readRequestHeader(isSecure);

        if (firstHeader.getMethod().equalsIgnoreCase(HttpRequestHeader.CONNECT)) {

            // ZAP: added host name variable
            String hostName = firstHeader.getHostName();
            try {
                httpOut.write(CONNECT_HTTP_200);
                httpOut.flush();

                byte[] bytes = new byte[3];
                bufferedInputStream.mark(3);
                bufferedInputStream.read(bytes);
                bufferedInputStream.reset();

                if (isSslTlsHandshake(bytes)) {
                    isSecure = true;
                    beginSSL(hostName);
                }

                firstHeader = httpIn.readRequestHeader(isSecure);
                processHttp(firstHeader, isSecure);
            } catch (MissingRootCertificateException e) {
                // Unluckily Firefox and Internet Explorer will not show this message.
                // We should find a way to let the browsers display this error message.
                // May we can redirect to some kind of ZAP custom error page.

                final HttpMessage errmsg = new HttpMessage(firstHeader);
                setErrorResponse(errmsg, BAD_GATEWAY_RESPONSE_STATUS, e, "ZAP SSL Error");

                writeHttpResponse(errmsg, httpOut);

                throw new IOException(e);
            }
        } else {
            processHttp(firstHeader, isSecure);
        }
    } catch (SocketTimeoutException e) {
        // ZAP: Log the exception
        if (firstHeader != null) {
            log.warn("Timeout accessing " + firstHeader.getURI());
        } else {
            log.warn("Timeout", e);
        }
    } catch (HttpMalformedHeaderException e) {
        log.warn("Malformed Header: ", e);
    } catch (HttpException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.debug("IOException: ", e);
    } finally {
        proxyThreadList.remove(thread);

        // ZAP: do only close if flag is false
        if (!keepSocketOpen) {
            disconnect();
        }
    }
}