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 void readFully(final InputStream fd, final byte[] dst, int off, int len) throws IOException 

Source Link

Document

Read the entire byte array into memory, or throw an exception.

Usage

From source file:com.google.gerrit.acceptance.ssh.UploadArchiveIT.java

License:Apache License

@Test
public void zipFormat() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommitId().abbreviate(8).name();
    String c = command(r, abbreviated);

    InputStream out = sshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));

    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();/*from w w w .  java 2s. co  m*/

    // Skip length (4 bytes) + 1 byte
    // to position the output stream to the raw zip stream
    byte[] buffer = new byte[5];
    IO.readFully(out, buffer, 0, 5);
    Set<String> entryNames = new TreeSet<>();
    try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
        ZipArchiveEntry zipEntry = zip.getNextZipEntry();
        while (zipEntry != null) {
            String name = zipEntry.getName();
            entryNames.add(name);
            zipEntry = zip.getNextZipEntry();
        }
    }

    assertThat(entryNames.size()).isEqualTo(1);
    assertThat(Iterables.getOnlyElement(entryNames))
            .isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}

From source file:com.google.gerrit.httpd.plugins.HttpPluginServlet.java

License:Apache License

private static byte[] readWholeEntry(PluginContentScanner scanner, PluginEntry entry) throws IOException {
    byte[] data = new byte[entry.getSize().get().intValue()];
    try (InputStream in = scanner.getInputStream(entry)) {
        IO.readFully(in, data, 0, data.length);
    }//  w ww .j  a  va 2 s  .  co  m
    return data;
}

From source file:com.google.gerrit.server.contact.EncryptedContactStore.java

License:Apache License

public void store(final Account account, final ContactInformation info)
        throws ContactInformationStoreException {
    try {/*from  www .j a  v  a  2s  .  c om*/
        final byte[] plainText = format(account, info).getBytes("UTF-8");
        final String fileName = "account-" + account.getId();
        final Date fileDate = account.getContactFiledOn();
        final byte[] encText = encrypt(fileName, fileDate, plainText);
        final String encStr = new String(encText, "UTF-8");

        final Timestamp filedOn = account.getContactFiledOn();
        final UrlEncoded u = new UrlEncoded();
        if (storeAPPSEC != null) {
            u.put("APPSEC", storeAPPSEC);
        }
        if (account.getPreferredEmail() != null) {
            u.put("email", account.getPreferredEmail());
        }
        if (filedOn != null) {
            u.put("filed", String.valueOf(filedOn.getTime() / 1000L));
        }
        u.put("account_id", String.valueOf(account.getId().get()));
        u.put("data", encStr);
        final byte[] body = u.toString().getBytes("UTF-8");

        final HttpURLConnection c = (HttpURLConnection) storeUrl.openConnection();
        c.setRequestMethod("POST");
        c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        c.setDoOutput(true);
        c.setFixedLengthStreamingMode(body.length);
        final OutputStream out = c.getOutputStream();
        out.write(body);
        out.close();

        if (c.getResponseCode() == 200) {
            final byte[] dst = new byte[2];
            final InputStream in = c.getInputStream();
            try {
                IO.readFully(in, dst, 0, 2);
            } finally {
                in.close();
            }
            if (dst[0] != 'O' || dst[1] != 'K') {
                throw new IOException("Store failed: " + c.getResponseCode());
            }
        } else {
            throw new IOException("Store failed: " + c.getResponseCode());
        }

    } catch (IOException e) {
        log.error("Cannot store encrypted contact information", e);
        throw new ContactInformationStoreException(e);
    } catch (PGPException e) {
        log.error("Cannot store encrypted contact information", e);
        throw new ContactInformationStoreException(e);
    } catch (NoSuchProviderException e) {
        log.error("Cannot store encrypted contact information", e);
        throw new ContactInformationStoreException(e);
    }
}

From source file:com.google.gerrit.server.plugins.AutoRegisterModules.java

License:Apache License

private byte[] read(JarEntry entry) throws IOException {
    byte[] data = new byte[(int) entry.getSize()];
    InputStream in = jarFile.getInputStream(entry);
    try {//w w  w .  j  a  v  a 2s  .co m
        IO.readFully(in, data, 0, data.length);
    } finally {
        in.close();
    }
    return data;
}

From source file:com.google.gerrit.server.plugins.JarScanner.java

License:Apache License

private static byte[] read(JarFile jarFile, JarEntry entry) throws IOException {
    byte[] data = new byte[(int) entry.getSize()];
    try (InputStream in = jarFile.getInputStream(entry)) {
        IO.readFully(in, data, 0, data.length);
    }/*  w w w  .ja va  2s  .c  om*/
    return data;
}

From source file:it.com.atlassian.labs.speakeasy.util.jgit.FixedTransportHttp.java

License:Eclipse Distribution License

private void readSmartHeaders(final InputStream in, final String service) throws IOException {
    // A smart reply will have a '#' after the first 4 bytes, but
    // a dumb reply cannot contain a '#' until after byte 41. Do a
    // quick check to make sure its a smart reply before we parse
    // as a pkt-line stream.
    ////  ww w. j ava  2  s .c  o  m
    final byte[] magic = new byte[5];
    IO.readFully(in, magic, 0, magic.length);
    if (magic[4] != '#') {
        throw new TransportException(uri,
                MessageFormat.format(JGitText.get().expectedPktLineWithService, RawParseUtils.decode(magic)));
    }

    final PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(new ByteArrayInputStream(magic), in));
    final String exp = "# service=" + service; //$NON-NLS-1$
    final String act = pckIn.readString();
    if (!exp.equals(act)) {
        throw new TransportException(uri, MessageFormat.format(JGitText.get().expectedGot, exp, act));
    }

    while (pckIn.readString() != PacketLineIn.END) {
        // for now, ignore the remaining header lines
    }
}