Example usage for java.util.zip Adler32 Adler32

List of usage examples for java.util.zip Adler32 Adler32

Introduction

In this page you can find the example usage for java.util.zip Adler32 Adler32.

Prototype

public Adler32() 

Source Link

Document

Creates a new Adler32 object.

Usage

From source file:org.olat.core.util.vfs.version.VersionsFileManager.java

private boolean isSameFile(VFSLeaf currentFile, VersionsFileImpl versions) {
    boolean same = false;
    if (versions.getRevisions() != null && !versions.getRevisions().isEmpty()) {
        VFSRevision lastRevision = versions.getRevisions().get(versions.getRevisions().size() - 1);

        long lastSize = lastRevision.getSize();
        long currentSize = currentFile.getSize();
        if (currentSize == lastSize && currentSize > 0 && lastRevision instanceof RevisionFileImpl
                && currentFile instanceof LocalFileImpl) {
            RevisionFileImpl lastRev = ((RevisionFileImpl) lastRevision);
            LocalFileImpl current = (LocalFileImpl) currentFile;
            //can be the same file
            try {
                Checksum cm1 = FileUtils.checksum(((LocalFileImpl) lastRev.getFile()).getBasefile(),
                        new Adler32());
                Checksum cm2 = FileUtils.checksum(current.getBasefile(), new Adler32());
                same = cm1.getValue() == cm2.getValue();
            } catch (IOException e) {
                log.debug("Error calculating the checksum of files");
            }//from   ww w. j a v a2  s  .  c om
        }
    }
    return same;
}

From source file:com.google.gwt.resources.rg.GssResourceGenerator.java

private String computeDefaultPrefix(ResourceContext context) {
    SortedSet<JClassType> gssResources = computeOperableTypes(context);

    Adler32 checksum = new Adler32();

    for (JClassType type : gssResources) {
        checksum.update(Util.getBytes(type.getQualifiedSourceName()));
    }//from  w  ww .j  a  v  a2 s .c  o  m

    int seed = Math.abs((int) checksum.getValue());

    return encode(seed) + "-";
}

From source file:org.sakaiproject.metaobj.shared.mgt.impl.StructuredArtifactDefinitionManagerImpl.java

/**
 * This method will export a form into a stream.  It has the ability to turn off checking
 * for the export form permission.// ww  w  . j av a 2  s.c om
 * @param formId String
 * @param os OutputStream
 * @param checkPermission boolean
 * @throws IOException
 */
public void packageFormForExport(String formId, OutputStream os, boolean checkPermission) throws IOException {
    if (checkPermission) {
        getAuthzManager().checkPermission(SharedFunctionConstants.EXPORT_ARTIFACT_DEF, getToolId());
    }

    CheckedOutputStream checksum = new CheckedOutputStream(os, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

    StructuredArtifactDefinitionBean bean = loadHome(formId);
    writeSADtoZip(bean, zos, "");

    zos.finish();
    zos.flush();
}

From source file:com.adito.core.CoreUtil.java

/**
 * @param f/*  w  w  w. j a v a  2 s .c  om*/
 * @return long
 * @throws IOException
 */
public static long generateChecksum(File f) throws IOException {
    Adler32 alder = new Adler32();
    FileInputStream fin = new FileInputStream(f);
    CheckedInputStream in = new CheckedInputStream(fin, alder);
    byte[] buf = new byte[32768];
    Util.readFullyIntoBuffer(in, buf);
    alder = (Adler32) in.getChecksum();
    try {
        in.close();
    } catch (IOException ex) {
    }
    try {
        fin.close();
    } catch (IOException ex1) {
    }
    return alder.getValue();
}

From source file:org.olat.core.util.mail.manager.MailManagerImpl.java

protected DBMail saveDBMessage(MailContext context, Identity fromId, String from, Identity toId, String to,
        Identity cc, List<ContactList> bccLists, String metaId, MailContent content, MailerResult result) {

    try {/* w  w  w.  j a  va  2 s  .  co m*/
        DBMailImpl mail = new DBMailImpl();
        if (result == null) {
            result = new MailerResult();
        }

        boolean makeRealMail = makeRealMail(toId, cc, bccLists);
        Address fromAddress = null;
        List<Address> toAddress = new ArrayList<Address>();
        List<Address> ccAddress = new ArrayList<Address>();
        List<Address> bccAddress = new ArrayList<Address>();

        if (fromId != null) {
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setRecipient(fromId);
            if (StringHelper.containsNonWhitespace(from)) {
                fromRecipient.setEmailAddress(from);
                fromAddress = createFromAddress(from, result);
            } else {
                fromAddress = createFromAddress(fromId, result);
            }
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            fromRecipient.setDeleted(Boolean.FALSE);
            mail.setFrom(fromRecipient);
        } else {
            if (!StringHelper.containsNonWhitespace(from)) {
                from = WebappHelper.getMailConfig("mailFrom");
            }
            DBMailRecipient fromRecipient = new DBMailRecipient();
            fromRecipient.setEmailAddress(from);
            fromRecipient.setVisible(Boolean.TRUE);
            fromRecipient.setMarked(Boolean.FALSE);
            fromRecipient.setDeleted(Boolean.TRUE);//marked as delted as nobody can read it
            mail.setFrom(fromRecipient);
            fromAddress = createFromAddress(from, result);
        }

        if (result.getReturnCode() != MailerResult.OK) {
            return null;
        }

        mail.setMetaId(metaId);
        String subject = content.getSubject();
        if (subject != null && subject.length() > 500) {
            logWarn("Cut a too long subkect in name. Size: " + subject.length(), null);
            subject = subject.substring(0, 500);
        }
        mail.setSubject(subject);
        String body = content.getBody();
        if (body != null && body.length() > 16777210) {
            logWarn("Cut a too long body in mail. Size: " + body.length(), null);
            body = body.substring(0, 16000000);
        }
        mail.setBody(body);
        mail.setLastModified(new Date());

        if (context != null) {
            OLATResourceable ores = context.getOLATResourceable();
            if (ores != null) {
                String resName = ores.getResourceableTypeName();
                if (resName != null && resName.length() > 50) {
                    logWarn("Cut a too long resourceable type name in mail context: " + resName, null);
                    resName = resName.substring(0, 49);
                }
                mail.getContext().setResName(ores.getResourceableTypeName());
                mail.getContext().setResId(ores.getResourceableId());
            }

            String resSubPath = context.getResSubPath();
            if (resSubPath != null && resSubPath.length() > 2000) {
                logWarn("Cut a too long resSubPath in mail context: " + resSubPath, null);
                resSubPath = resSubPath.substring(0, 2000);
            }
            mail.getContext().setResSubPath(resSubPath);

            String businessPath = context.getBusinessPath();
            if (businessPath != null && businessPath.length() > 2000) {
                logWarn("Cut a too long resSubPath in mail context: " + businessPath, null);
                businessPath = businessPath.substring(0, 2000);
            }
            mail.getContext().setBusinessPath(businessPath);
        }

        //add to
        DBMailRecipient recipientTo = null;
        if (toId != null) {
            recipientTo = new DBMailRecipient();
            if (toId instanceof PersistentObject) {
                recipientTo.setRecipient(toId);
            } else {
                to = toId.getUser().getProperty(UserConstants.EMAIL, null);
            }
            if (StringHelper.containsNonWhitespace(to)) {
                recipientTo.setEmailAddress(to);
            }
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.FALSE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        } else if (StringHelper.containsNonWhitespace(to)) {
            recipientTo = new DBMailRecipient();
            recipientTo.setEmailAddress(to);
            recipientTo.setVisible(Boolean.TRUE);
            recipientTo.setDeleted(Boolean.TRUE);
            recipientTo.setMarked(Boolean.FALSE);
            recipientTo.setRead(Boolean.FALSE);
        }

        if (recipientTo != null) {
            mail.getRecipients().add(recipientTo);
            createAddress(toAddress, recipientTo, true, result, true);
        }
        if (makeRealMail && StringHelper.containsNonWhitespace(to)) {
            createAddress(toAddress, to);
        }

        if (cc != null) {
            DBMailRecipient recipient = new DBMailRecipient();
            if (cc instanceof PersistentObject) {
                recipient.setRecipient(cc);
            } else {
                recipient.setEmailAddress(cc.getUser().getProperty(UserConstants.EMAIL, null));
            }
            recipient.setVisible(Boolean.TRUE);
            recipient.setDeleted(Boolean.FALSE);
            recipient.setMarked(Boolean.FALSE);
            recipient.setRead(Boolean.FALSE);
            mail.getRecipients().add(recipient);
            createAddress(ccAddress, recipient, false, result, true);
        }

        //add bcc recipients
        appendRecipients(mail, bccLists, toAddress, bccAddress, false, makeRealMail, result);

        dbInstance.getCurrentEntityManager().persist(mail);

        //save attachments
        List<File> attachments = content.getAttachments();
        if (attachments != null && !attachments.isEmpty()) {
            for (File attachment : attachments) {

                FileInputStream in = null;
                try {
                    DBMailAttachment data = new DBMailAttachment();
                    data.setSize(attachment.length());
                    data.setName(attachment.getName());

                    long checksum = FileUtils.checksum(attachment, new Adler32()).getValue();
                    data.setChecksum(new Long(checksum));
                    data.setMimetype(WebappHelper.getMimeType(attachment.getName()));

                    in = new FileInputStream(attachment);
                    String path = saveAttachmentToStorage(data.getName(), data.getMimetype(), checksum,
                            attachment.length(), in);
                    data.setPath(path);
                    data.setMail(mail);

                    dbInstance.getCurrentEntityManager().persist(data);
                } catch (FileNotFoundException e) {
                    logError("File attachment not found: " + attachment, e);
                } catch (IOException e) {
                    logError("Error with file attachment: " + attachment, e);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }

        if (makeRealMail) {
            //check that we send an email to someone
            if (!toAddress.isEmpty() || !ccAddress.isEmpty() || !bccAddress.isEmpty()) {
                sendRealMessage(fromAddress, toAddress, ccAddress, bccAddress, subject, body, attachments,
                        result);
            }
        }

        //update subscription
        for (DBMailRecipient recipient : mail.getRecipients()) {
            if (recipient.getRecipient() != null) {
                subscribe(recipient.getRecipient());
            }
        }

        SubscriptionContext subContext = getSubscriptionContext();
        notificationsManager.markPublisherNews(subContext, null, false);
        return mail;
    } catch (AddressException e) {
        logError("Cannot send e-mail: ", e);
        result.setReturnCode(MailerResult.RECIPIENT_ADDRESS_ERROR);
        return null;
    }
}

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String Zip(String zipFileName, String srcName) {
    String fixedZipFileName = fixFileName(zipFileName);
    String fixedSrcName = fixFileName(srcName);
    String sRet = "";

    try {//from   ww  w .j a v  a2  s  . co m
        FileOutputStream dest = new FileOutputStream(fixedZipFileName);
        CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));
        out.setMethod(ZipOutputStream.DEFLATED);

        sRet += AddFilesToZip(out, fixedSrcName, "");

        out.close();
        System.out.println("checksum:                   " + checksum.getChecksum().getValue());
        sRet += "checksum:                   " + checksum.getChecksum().getValue();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return (sRet);
}

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String Unzip(String zipFileName, String dstDirectory) {
    String sRet = "";
    String fixedZipFileName = fixFileName(zipFileName);
    String fixedDstDirectory = fixFileName(dstDirectory);
    String dstFileName = "";
    int nNumExtracted = 0;
    boolean bRet = false;

    try {//  w  w  w  .  j av a  2s.  com
        final int BUFFER = 2048;
        BufferedOutputStream dest = null;
        ZipFile zipFile = new ZipFile(fixedZipFileName);
        int nNumEntries = zipFile.size();
        zipFile.close();

        FileInputStream fis = new FileInputStream(fixedZipFileName);
        CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
        ZipEntry entry;

        byte[] data = new byte[BUFFER];

        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            if (fixedDstDirectory.length() > 0)
                dstFileName = fixedDstDirectory + entry.getName();
            else
                dstFileName = entry.getName();

            String tmpDir = dstFileName.substring(0, dstFileName.lastIndexOf('/'));
            File tmpFile = new File(tmpDir);
            if (!tmpFile.exists()) {
                bRet = tmpFile.mkdirs();
            } else
                bRet = true;

            if (bRet) {
                // if we aren't just creating a directory
                if (dstFileName.lastIndexOf('/') != (dstFileName.length() - 1)) {
                    // write out the file
                    FileOutputStream fos = new FileOutputStream(dstFileName);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                    dest = null;
                    fos.close();
                    fos = null;
                }
                nNumExtracted++;
            } else
                sRet += " - failed" + lineSep;
        }

        data = null;
        zis.close();
        System.out.println("Checksum:          " + checksum.getChecksum().getValue());
        sRet += "Checksum:          " + checksum.getChecksum().getValue();
        sRet += lineSep + nNumExtracted + " of " + nNumEntries + " successfully extracted";
    } catch (Exception e) {
        e.printStackTrace();
    }

    return (sRet);
}