Example usage for com.google.common.io CharStreams readLines

List of usage examples for com.google.common.io CharStreams readLines

Introduction

In this page you can find the example usage for com.google.common.io CharStreams readLines.

Prototype

public static List<String> readLines(Readable r) throws IOException 

Source Link

Document

Reads all of the lines from a Readable object.

Usage

From source file:net.minecraftforge.gradle.common.Constants.java

public static List<String> lines(final String text) {
    try {//from ww  w .  j av  a 2s  . c  o m
        return ImmutableList.copyOf(CharStreams.readLines(new StringReader(text)));
    } catch (IOException e) {
        // IMPOSSIBRU
        return ImmutableList.of();
    }
}

From source file:org.eclipse.che.api.builder.internal.SourcesManagerImpl.java

private void download(String downloadUrl, java.io.File downloadTo) throws IOException {
    HttpURLConnection conn = null;
    try {/*  w w  w  .j  a  v a 2s. c o  m*/
        final LinkedList<java.io.File> q = new LinkedList<>();
        q.add(downloadTo);
        final long start = System.currentTimeMillis();
        final List<Pair<String, String>> md5sums = new LinkedList<>();
        while (!q.isEmpty()) {
            java.io.File current = q.pop();
            java.io.File[] list = current.listFiles();
            if (list != null) {
                for (java.io.File f : list) {
                    if (f.isDirectory()) {
                        q.push(f);
                    } else {
                        md5sums.add(Pair.of(com.google.common.io.Files.hash(f, Hashing.md5()).toString(),
                                downloadTo.toPath().relativize(f.toPath()).toString().replace("\\", "/"))); //Replacing of "\" is need for windows support
                    }
                }
            }
        }
        final long end = System.currentTimeMillis();
        if (md5sums.size() > 0) {
            LOG.debug("count md5sums of {} files, time: {}ms", md5sums.size(), (end - start));
        }
        conn = (HttpURLConnection) new URL(downloadUrl).openConnection();
        conn.setConnectTimeout(CONNECT_TIMEOUT);
        conn.setReadTimeout(READ_TIMEOUT);
        final EnvironmentContext context = EnvironmentContext.getCurrent();
        if (context.getUser() != null && context.getUser().getToken() != null) {
            conn.setRequestProperty(HttpHeaders.AUTHORIZATION, context.getUser().getToken());
        }
        if (!md5sums.isEmpty()) {
            conn.setRequestMethod(HttpMethod.POST);
            conn.setRequestProperty("Content-type", MediaType.TEXT_PLAIN);
            conn.setRequestProperty(HttpHeaders.ACCEPT, MediaType.MULTIPART_FORM_DATA);
            conn.setDoOutput(true);
            try (OutputStream output = conn.getOutputStream(); Writer writer = new OutputStreamWriter(output)) {
                for (Pair<String, String> pair : md5sums) {
                    writer.write(pair.first);
                    writer.write(' ');
                    writer.write(pair.second);
                    writer.write('\n');
                }
            }
        }
        final int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            final String contentType = conn.getHeaderField("content-type");
            if (contentType.startsWith(MediaType.MULTIPART_FORM_DATA)) {
                final HeaderParameterParser headerParameterParser = new HeaderParameterParser();
                final String boundary = headerParameterParser.parse(contentType).get("boundary");
                try (InputStream in = conn.getInputStream()) {
                    MultipartStream multipart = new MultipartStream(in, boundary.getBytes());
                    boolean hasMore = multipart.skipPreamble();
                    while (hasMore) {
                        final Map<String, List<String>> headers = parseChunkHeader(
                                CharStreams.readLines(new StringReader(multipart.readHeaders())));
                        final List<String> contentDisposition = headers.get("content-disposition");
                        final String name = headerParameterParser.parse(contentDisposition.get(0)).get("name");
                        if ("updates".equals(name)) {
                            int length = -1;
                            List<String> contentLengthHeader = headers.get("content-length");
                            if (contentLengthHeader != null && !contentLengthHeader.isEmpty()) {
                                length = Integer.parseInt(contentLengthHeader.get(0));
                            }
                            if (length < 0 || length > 204800) {
                                java.io.File tmp = java.io.File.createTempFile("tmp", ".zip", directory);
                                try {
                                    try (FileOutputStream fOut = new FileOutputStream(tmp)) {
                                        multipart.readBodyData(fOut);
                                    }
                                    ZipUtils.unzip(tmp, downloadTo);
                                } finally {
                                    if (tmp.exists()) {
                                        tmp.delete();
                                    }
                                }
                            } else {
                                final ByteArrayOutputStream bOut = new ByteArrayOutputStream(length);
                                multipart.readBodyData(bOut);
                                ZipUtils.unzip(new ByteArrayInputStream(bOut.toByteArray()), downloadTo);
                            }
                        } else if ("removed-paths".equals(name)) {
                            final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
                            multipart.readBodyData(bOut);
                            final String[] removed = JsonHelper.fromJson(
                                    new ByteArrayInputStream(bOut.toByteArray()), String[].class, null);
                            for (String path : removed) {
                                java.io.File f = new java.io.File(downloadTo, path);
                                if (!f.delete()) {
                                    throw new IOException(String.format("Unable delete %s", path));
                                }
                            }
                        } else {
                            // To /dev/null :)
                            multipart.readBodyData(DEV_NULL);
                        }
                        hasMore = multipart.readBoundary();
                    }
                }
            } else {
                try (InputStream in = conn.getInputStream()) {
                    ZipUtils.unzip(in, downloadTo);
                }
            }
        } else if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) {
            throw new IOException(
                    String.format("Invalid response status %d from remote server. ", responseCode));
        }
    } catch (ParseException | JsonParseException e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:com.google.gxp.base.dynamic.StubGxpTemplate.java

/**
 * Examine each element of the stack trace that belongs to this throwable
 * looking for a filename that matches the source file for this template.
 * If we find a match, rewrite the filename and line number. The line number
 * is based on line # comments in the source file.
 *//*from   w ww.jav  a  2  s  .  c  o m*/
protected static void rewriteStackTraceElements(Throwable throwable, FileRef sourceFile) {
    try {
        if (sourceFile != null) {
            String sourceFileName = sourceFile.getName().substring(1).replace('/', '.');
            StackTraceElement[] stackTrace = throwable.getStackTrace();
            for (int i = 0; i < stackTrace.length; i++) {
                if (sourceFileName.equals(stackTrace[i].getFileName())) {
                    // get source name
                    String[] parts = sourceFileName.split("[\\.\\$]");
                    String sourceName = parts[parts.length - 3] + ".gxp";

                    // get source line
                    String line = CharStreams.readLines(sourceFile.openReader(Charsets.UTF_8))
                            .get(stackTrace[i].getLineNumber() - 1);
                    Matcher m = LINE_DIRECTIVE.matcher(line);
                    int sourceLine = m.find() ? Integer.valueOf(m.group(2)) : -1;

                    // fix class name
                    String className = stackTrace[i].getClassName().split("\\$")[0];

                    stackTrace[i] = new StackTraceElement(className, stackTrace[i].getMethodName(), sourceName,
                            sourceLine);
                    throwable.setStackTrace(stackTrace);
                    return;
                }
            }
        }
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:org.apache.wave.pst.PstFileDescriptor.java

private List<String> readLines(InputStream is) {
    try {/*  www.  j a  v a  2s.c  o m*/
        return CharStreams.readLines(new InputStreamReader(is));
    } catch (IOException e) {
        e.printStackTrace();
        // TODO(kalman): this is a bit hacky, deal with it properly.
        return Collections.singletonList("(Error, couldn't read lines from the input stream. "
                + "Try running the command external to PST to view the output.)");
    }
}

From source file:gov.nasa.jpl.magicdraw.projectUsageIntegrity.ProjectUsageIntegrityPlugin.java

@Override
public void init() {
    MDLog.getPluginsLog().info("INIT: >> " + getPluginName());

    final PluginDescriptor pluginDescriptor = this.getDescriptor();

    File pluginDir = pluginDescriptor.getPluginDirectory();

    sscaeProjectUsageIntegrityIconsFolderPath = pluginDir.getAbsolutePath() + File.separator + "icons"
            + File.separator;//from w ww  . j a  v  a2 s . c  o m

    try {
        String javaSpecificationVersion = System.getProperty("java.specification.version");
        if (!"1.6".equals(javaSpecificationVersion) && !"1.7".equals(javaSpecificationVersion)
                && !"1.8".equals(javaSpecificationVersion)) {
            StringBuffer buff = new StringBuffer();
            buff.append(
                    "*** SSCAE supports running MagicDraw with Java 1.6 (recommended), 1.7 or 1.8 at JPL ***\n");
            for (String property : JAVA_PROPERTIES) {
                buff.append(String.format(PROPERTY_NAME_VALUE_FORMAT, property, System.getProperty(property)));
            }

            buff.append("\n\nFor questions, contact SSCAE: https://sscae-help.jpl.nasa.gov/contacts.php");
            buff.append("\n\n*** OK to exit?");
            int okToExit = JOptionPane.showConfirmDialog(null, buff.toString(), "Java Version Mismatch",
                    JOptionPane.OK_CANCEL_OPTION);
            if (okToExit == JOptionPane.OK_OPTION) {
                System.exit(-1);
            }
        }
        ProjectUsageIntegrityPlugin.INSTANCE = this;

        if (QVTOPlugin.getInstance() == null) {
            MDLog.getPluginsLog().fatal("The QVTO Library plugin should have been initialized!");
        }

        try {
            SSCAE_CLEAN_MD5_TAG_REPLACE = Pattern.compile(SSCAE_CLEAN_MD5_TAG_PATTERN);
        } catch (PatternSyntaxException e) {
            MDLog.getPluginsLog().fatal(String.format(
                    "INIT: -- %s - cannot compile the SSCAE Clean tag regex pattern", getPluginName()), e);
            System.exit(-1);
        }

        MetamodelTransactionPropertyNameCache.initialize();

        this.mProjectEventListener = new ProjectUsageEventListenerAdapter(this);
        Application a = Application.getInstance();

        a.addProjectEventListener(this.mProjectEventListener);

        this.mSaveParticipant = new ProjectUsageSaveParticipant();
        a.addSaveParticipant(this.mSaveParticipant);

        EvaluationConfigurator.getInstance()
                .registerBinaryImplementers(ProjectUsageIntegrityPlugin.class.getClassLoader());

        ActionsConfiguratorsManager manager = ActionsConfiguratorsManager.getInstance();
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.projectUsageIntegrityToggleAction));
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.checkCurrentProjectStatusAction));
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.showCurrentProjectUsageGraphAction));
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.toggleTeamworkOrAllGraphAction));
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.toggleGraphLabelAction));
        manager.addMainToolbarConfigurator(new ToolbarConfigurator(this.toggleThreadedAction));

        final String logTraceContractsDir = pluginDescriptor.getPluginDirectory() + File.separator
                + "logTraceContracts" + File.separator;
        logTraceContractsFolder = new File(logTraceContractsDir);
        if (!logTraceContractsFolder.exists() || !logTraceContractsFolder.isDirectory()
                || !logTraceContractsFolder.canRead()) {
            MDLog.getPluginsLog()
                    .error(String.format("INIT: -- %s - cannot find the 'logTraceContracts' folder in: %s",
                            getPluginName(), logTraceContractsDir));
            logTraceContractsFolder = null;
        }

        options = new SSCAEProjectUsageIntegrityOptions();
        EnvironmentOptions envOptions = a.getEnvironmentOptions();
        envOptions.addGroup(options);

        logTraceContractsAppender = new Appender();

        Logger.getRootLogger().addAppender(logTraceContractsAppender);
        System.setErr(new PrintStream(new AppendingOutputStream(System.err, logTraceContractsAppender,
                MDLog.getPluginsLog(), Level.ERROR)));

        File suffixFile = new File(
                pluginDir.getAbsolutePath() + File.separator + MD_TEAMWORK_PROJECT_ID_SUFFIXES_FILE);
        if (suffixFile.exists() && suffixFile.canRead()) {
            try {
                InputStream is = suffixFile.toURI().toURL().openStream();
                List<String> lines = CharStreams.readLines(new InputStreamReader(is));
                for (String line : lines) {
                    if (line.startsWith("#"))
                        continue;
                    if (line.isEmpty())
                        continue;
                    MDTeamworkProjectIDSuffixes.add(line);
                    MDLog.getPluginsLog().info(String.format("MD teamwork project ID suffix: '%s'", line));
                }
            } catch (MalformedURLException e) {
                MDLog.getPluginsLog()
                        .error(String.format("Error reading the MD teamwork project ID suffix file: %s: %s",
                                suffixFile, e.getMessage()), e);
                MDTeamworkProjectIDSuffixes.clear();
            } catch (IOException e) {
                MDLog.getPluginsLog()
                        .error(String.format("Error reading the MD teamwork project ID suffix file: %s: %s",
                                suffixFile, e.getMessage()), e);
                MDTeamworkProjectIDSuffixes.clear();
            }
        }
    } finally {
        MDLog.getPluginsLog().info("INIT: << " + getPluginName());
    }
}

From source file:de.dentrassi.pm.storage.service.jpa.TransferHandler.java

private Set<String> getAspects(final ZipFile zip) throws IOException {
    final ZipEntry ze = zip.getEntry("aspects");
    if (ze == null) {
        return Collections.emptySet();
    }/*from www  .  j ava  2  s  .c  om*/

    try (InputStream stream = zip.getInputStream(ze)) {
        final List<String> lines = CharStreams.readLines(new InputStreamReader(stream, StandardCharsets.UTF_8));
        return new HashSet<>(lines);
    }
}

From source file:com.github.sdbg.debug.ui.internal.presentation.SDBGDebugModelPresentation.java

private String getLineExtract(ILineBreakpoint bp) {
    try {//  ww  w .j  a v a  2  s.  com
        Reader r = new InputStreamReader(((IFile) bp.getMarker().getResource()).getContents(),
                ((IFile) bp.getMarker().getResource()).getCharset());

        List<String> lines = CharStreams.readLines(r);

        int line = bp.getLineNumber() - 1;

        if (line > 0 && line < lines.size()) {
            String lineStr = lines.get(line).trim();

            return lineStr.length() == 0 ? null : lineStr;
        }
    } catch (IOException ioe) {
        return null;
    } catch (CoreException ce) {
        return null;
    }

    return null;
}

From source file:com.eucalyptus.keys.KeyPairManager.java

/**
 * Decode any supported key material./*from   ww  w  .j a v a2  s  .  c  o  m*/
 *
 * Supported formats:
 * OpenSSH public key format (e.g., the format in ~/.ssh/authorized_keys)  http://tools.ietf.org/html/rfc4253#section-6.6 
 * Base64 encoded DER format
 * SSH public key file format as specified in RFC4716
 *
 * Supported lengths: 1024, 2048, and 4096.
 * 
 * @return The RSA public key
 */
private static RSAPublicKey decodeKeyMaterial(final String b64EncodedKeyMaterial)
        throws GeneralSecurityException {
    final String decoded;
    try {
        decoded = B64.standard.decString(b64EncodedKeyMaterial);
    } catch (ArrayIndexOutOfBoundsException | StringIndexOutOfBoundsException | DecoderException e) {
        throw new GeneralSecurityException("Invalid key material (expected Base64)");
    }
    final RSAPublicKey key;
    if (decoded.startsWith("ssh-rsa ")) {
        final String[] keyParts = decoded.split("\\s+");
        if (keyParts.length > 1) {
            key = KeyPairs.decodeSshRsaPublicKey(keyParts[1]);
        } else {
            throw new GeneralSecurityException("Invalid SSH key format");
        }
    } else if (decoded.startsWith("---- BEGIN SSH2 PUBLIC KEY ----")) {
        final String keyB64;
        try {
            keyB64 = Joiner.on("\n").join(
                    Iterables.filter(CharStreams.readLines(new StringReader(decoded)), new Predicate<String>() {
                        boolean continueLine = false;
                        boolean sawEnd = false;

                        @Override
                        public boolean apply(final String line) {
                            // skip start / end lines
                            sawEnd = sawEnd || line.contains("---- END SSH2 PUBLIC KEY ----");
                            if (line.contains("---- BEGIN SSH2 PUBLIC KEY ----") || sawEnd) {
                                continueLine = false;
                                return false;
                            }

                            // skip headers and header continuations
                            if (continueLine || line.contains(":")) {
                                continueLine = line.endsWith("\\");
                                return false;
                            }

                            return true;
                        }
                    }));
        } catch (IOException e) {
            throw new GeneralSecurityException("Error reading key data");
        }
        key = KeyPairs.decodeSshRsaPublicKey(keyB64);
    } else {
        try {
            final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            final X509EncodedKeySpec encodedSpec = new X509EncodedKeySpec(B64.standard.dec(decoded));
            key = (RSAPublicKey) keyFactory.generatePublic(encodedSpec);
        } catch (ArrayIndexOutOfBoundsException | StringIndexOutOfBoundsException | DecoderException e) {
            throw new GeneralSecurityException("Invalid key material");
        }
    }

    // verify key properties
    final int keySize = key.getModulus().bitLength();
    if (keySize != 1024 && keySize != 2048 && keySize != 4096) {
        throw new GeneralSecurityException("Invalid key size: " + keySize);
    }

    return key;
}

From source file:org.apache.parquet.cli.BaseCommand.java

protected <D> Iterable<D> openDataFile(final String source, Schema projection) throws IOException {
    Formats.Format format = Formats.detectFormat(open(source));
    switch (format) {
    case PARQUET:
        Configuration conf = new Configuration(getConf());
        // TODO: add these to the reader builder
        AvroReadSupport.setRequestedProjection(conf, projection);
        AvroReadSupport.setAvroReadSchema(conf, projection);
        final ParquetReader<D> parquet = AvroParquetReader.<D>builder(qualifiedPath(source))
                .disableCompatibility().withDataModel(GenericData.get()).withConf(conf).build();
        return new Iterable<D>() {
            @Override//from   w  w  w  . jav  a 2s .  com
            public Iterator<D> iterator() {
                return new Iterator<D>() {
                    private boolean hasNext = false;
                    private D next = advance();

                    @Override
                    public boolean hasNext() {
                        return hasNext;
                    }

                    @Override
                    public D next() {
                        if (!hasNext) {
                            throw new NoSuchElementException();
                        }
                        D toReturn = next;
                        this.next = advance();
                        return toReturn;
                    }

                    private D advance() {
                        try {
                            D next = parquet.read();
                            this.hasNext = (next != null);
                            return next;
                        } catch (IOException e) {
                            throw new RuntimeException("Failed while reading Parquet file: " + source, e);
                        }
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("Remove is not supported");
                    }
                };
            }
        };

    case AVRO:
        Iterable<D> avroReader = (Iterable<D>) DataFileReader.openReader(openSeekable(source),
                new GenericDatumReader<>(projection));
        return avroReader;

    default:
        if (source.endsWith("json")) {
            return new AvroJsonReader<>(open(source), projection);
        } else {
            Preconditions.checkArgument(projection == null, "Cannot select columns from text files");
            Iterable text = CharStreams.readLines(new InputStreamReader(open(source)));
            return text;
        }
    }
}

From source file:io.takari.maven.plugins.compile.AbstractCompileMojo.java

private Proc getEffectiveProc(List<File> classpath) {
    Proc proc = this.proc;
    if (proc == null) {
        Multimap<File, String> processors = TreeMultimap.create();
        for (File jar : classpath) {
            if (jar.isFile()) {
                try (ZipFile zip = new ZipFile(jar)) {
                    ZipEntry entry = zip.getEntry("META-INF/services/javax.annotation.processing.Processor");
                    if (entry != null) {
                        try (Reader r = new InputStreamReader(zip.getInputStream(entry), Charsets.UTF_8)) {
                            processors.putAll(jar, CharStreams.readLines(r));
                        }/*from w w  w .j  a v a2 s . c o m*/
                    }
                } catch (IOException e) {
                    // ignore, compiler won't be able to use this jar either
                }
            } else if (jar.isDirectory()) {
                try {
                    processors.putAll(jar,
                            Files.readLines(
                                    new File(jar, "META-INF/services/javax.annotation.processing.Processor"),
                                    Charsets.UTF_8));
                } catch (IOException e) {
                    // ignore, compiler won't be able to use this jar either
                }
            }
        }
        if (!processors.isEmpty()) {
            StringBuilder msg = new StringBuilder(
                    "<proc> must be one of 'none', 'only' or 'proc'. Processors found: ");
            for (File jar : processors.keySet()) {
                msg.append("\n   ").append(jar).append(" ").append(processors.get(jar));
            }
            throw new IllegalArgumentException(msg.toString());
        }
        proc = Proc.none;
    }
    return proc;
}