Example usage for org.eclipse.jgit.util IO readFully

List of usage examples for org.eclipse.jgit.util IO readFully

Introduction

In this page you can find the example usage for org.eclipse.jgit.util IO readFully.

Prototype

public static final byte[] readFully(File path) throws FileNotFoundException, IOException 

Source Link

Document

Read an entire local file into memory as a byte array.

Usage

From source file:MySmartApply.java

License:Eclipse Distribution License

/**
 * Executes the {@code ApplyCommand} command with all the options and
 * parameters collected by the setter methods (e.g.
 * {@link #setPatch(InputStream)} of this class. Each instance of this class
 * should only be used for one invocation of the command. Don't call this
 * method twice on an instance./*from www.j  a  v  a2 s  .  c om*/
 *
 * @return an {@link ApplyResult} object representing the command result
 * @throws GitAPIException
 * @throws PatchFormatException
 * @throws PatchApplyException
 */
public ApplyResult call() throws GitAPIException, PatchFormatException, PatchApplyException {
    ApplyResult r = new ApplyResult();
    try {
        final Patch p = new Patch();
        try {
            p.parse(in);
        } finally {
            in.close();
        }
        if (!p.getErrors().isEmpty())
            throw new PatchFormatException(p.getErrors());
        for (FileHeader fh : p.getFiles()) {

            if (listener != null)
                listener.setFilename(
                        getFile((fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(),
                                false).getName());

            if (fh.getChangeType() == ChangeType.ADD || fh.getChangeType() == ChangeType.MODIFY
                    || fh.getChangeType() == ChangeType.COPY) {
                if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains(".gitignore")
                        || fh.getNewPath().equals("includes/config.php")
                        || fh.getNewPath().contains("/images")) {
                    if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains("/images"))
                        this.failed_files.add(getFile(
                                (fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(),
                                false));

                    if (listener != null)
                        listener.skipped();
                    continue;
                }
            }

            if (listener != null)
                listener.started();

            ChangeType type = fh.getChangeType();
            File f = null;
            switch (type) {
            case ADD:
                f = getFile(fh.getNewPath(), true);
                apply(f, fh);
                break;
            case MODIFY:
                f = getFile(fh.getOldPath(), false);
                apply(f, fh);
                break;
            case DELETE:
                f = getFile(fh.getOldPath(), false);
                if (!f.delete())
                    throw new PatchApplyException(MessageFormat.format(JGitText.get().cannotDeleteFile, f));
                break;
            case RENAME:
                f = getFile(fh.getOldPath(), false);
                File dest = getFile(fh.getNewPath(), false);
                if (!f.renameTo(dest))
                    throw new PatchApplyException(
                            MessageFormat.format(JGitText.get().renameFileFailed, f, dest));
                break;
            case COPY:
                f = getFile(fh.getOldPath(), false);
                byte[] bs = IO.readFully(f);
                FileOutputStream fos = new FileOutputStream(getFile(fh.getNewPath(), true));
                try {
                    fos.write(bs);
                } finally {
                    fos.close();
                }
            }

            // ... //

            if (!failed_files.contains(f) && type != ChangeType.DELETE)
                r.addUpdatedFile(f);

            // ... //
        }
    } catch (IOException e) {
        throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, e.getMessage()),
                e);
    }

    return r;
}

From source file:com.google.gerrit.common.FileUtil.java

License:Apache License

public static boolean modified(FileBasedConfig cfg) throws IOException {
    byte[] curVers;
    try {/* w w  w.j av  a 2s.  c om*/
        curVers = IO.readFully(cfg.getFile());
    } catch (FileNotFoundException notFound) {
        return true;
    }

    byte[] newVers = Constants.encode(cfg.toText());
    return !Arrays.equals(curVers, newVers);
}

From source file:com.google.gerrit.httpd.gitweb.GitLogoServlet.java

License:Apache License

@Inject
GitLogoServlet(final GitWebConfig gitWebConfig) throws IOException {
    byte[] png;/*from   www . ja v a  2s.  c  o m*/
    final File src = gitWebConfig.getGitLogoPNG();
    if (src != null) {
        try {
            png = IO.readFully(src);
        } catch (FileNotFoundException e) {
            png = null;
        }
        modified = src.lastModified();
    } else {
        modified = -1;
        png = null;
    }
    raw = png;
}

From source file:com.google.gerrit.httpd.gitweb.GitWebJavaScriptServlet.java

License:Apache License

@Inject
GitWebJavaScriptServlet(final GitWebConfig gitWebConfig) throws IOException {
    byte[] png;/*from   w w  w. j  ava 2s. c om*/
    final File src = gitWebConfig.getGitwebJS();
    if (src != null) {
        try {
            png = IO.readFully(src);
        } catch (FileNotFoundException e) {
            png = null;
        }
        modified = src.lastModified();
    } else {
        modified = -1;
        png = null;
    }
    raw = png;
}

From source file:com.google.gerrit.pgm.init.InitUtil.java

License:Apache License

private static boolean modified(FileBasedConfig cfg) throws IOException {
    byte[] curVers;
    try {//from   w w w.  j  a v a2  s. c om
        curVers = IO.readFully(cfg.getFile());
    } catch (FileNotFoundException notFound) {
        return true;
    }

    byte[] newVers = Constants.encode(cfg.toText());
    return !Arrays.equals(curVers, newVers);
}

From source file:com.google.gerrit.pgm.init.InitUtil.java

License:Apache License

static void copy(final File dst, final ByteBuffer buf) throws FileNotFoundException, IOException {
    // If the file already has the content we want to put there,
    // don't attempt to overwrite the file.
    ////  ww w.j  a v a 2  s .c om
    try {
        if (buf.equals(ByteBuffer.wrap(IO.readFully(dst)))) {
            return;
        }
    } catch (FileNotFoundException notFound) {
        // Fall through and write the file.
    }

    dst.getParentFile().mkdirs();
    LockFile lf = new LockFile(dst, FS.DETECTED);
    if (!lf.lock()) {
        throw new IOException("Cannot lock " + dst);
    }
    try {
        final OutputStream out = lf.getOutputStream();
        try {
            final byte[] tmp = new byte[4096];
            while (0 < buf.remaining()) {
                int n = Math.min(buf.remaining(), tmp.length);
                buf.get(tmp, 0, n);
                out.write(tmp, 0, n);
            }
        } finally {
            out.close();
        }
        if (!lf.commit()) {
            throw new IOException("Cannot commit " + dst);
        }
    } finally {
        lf.unlock();
    }
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private String getProjectDescription(final Repository e) throws IOException {
    final File d = new File(e.getDirectory(), "description");

    String description;//from   ww w  .  ja  v a  2 s .  co  m
    try {
        description = RawParseUtils.decode(IO.readFully(d));
    } catch (FileNotFoundException err) {
        return null;
    }

    if (description != null) {
        description = description.trim();
        if (description.isEmpty()) {
            description = null;
        }
        if (UNNAMED.equals(description)) {
            description = null;
        }
    }
    return description;
}

From source file:com.google.gitiles.DefaultAccess.java

License:Open Source License

private String loadDescriptionText(Repository repo) throws IOException {
    String desc = null;//from w  ww  . j a  va2s  . c o  m
    StoredConfig config = repo.getConfig();
    IOException configError = null;
    try {
        config.load();
        desc = config.getString("gitweb", null, "description");
    } catch (ConfigInvalidException e) {
        configError = new IOException(e);
    }
    if (desc == null) {
        File descFile = new File(repo.getDirectory(), "description");
        if (descFile.exists()) {
            desc = new String(IO.readFully(descFile));
        } else if (configError != null) {
            throw configError;
        }
    }
    return desc;
}

From source file:org.eclipse.egit.core.test.GitTestCase.java

License:Open Source License

protected ObjectId createFile(Repository repository, IProject actProject, String name, String content)
        throws IOException {
    File file = new File(actProject.getProject().getLocation().toFile(), name);
    FileWriter fileWriter = new FileWriter(file);
    fileWriter.write(content);/*  www.j  a  va 2s. c o  m*/
    fileWriter.close();
    byte[] fileContents = IO.readFully(file);
    ObjectInserter inserter = repository.newObjectInserter();
    try {
        ObjectId objectId = inserter.insert(Constants.OBJ_BLOB, fileContents);
        inserter.flush();
        return objectId;
    } finally {
        inserter.release();
    }
}

From source file:org.eclipse.egit.core.test.GitTestCase.java

License:Open Source License

protected ObjectId createFileCorruptShort(Repository repository, IProject actProject, String name,
        String content) throws IOException {
    ObjectId id = createFile(repository, actProject, name, content);
    File file = new File(repository.getDirectory(),
            "objects/" + id.name().substring(0, 2) + "/" + id.name().substring(2));
    byte[] readFully = IO.readFully(file);
    FileUtils.delete(file);/*  w  w  w. j av a2 s  .com*/
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    byte[] truncatedData = new byte[readFully.length - 1];
    System.arraycopy(readFully, 0, truncatedData, 0, truncatedData.length);
    fileOutputStream.write(truncatedData);
    fileOutputStream.close();
    return id;
}