Ejemplo n.º 1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void PutImpl(string bucket, string key, byte[] csum, TemporaryBuffer buf,
                             ProgressMonitor monitor, string monitorTask)
        {
            if (monitor == null)
            {
                monitor = NullProgressMonitor.INSTANCE;
            }
            if (monitorTask == null)
            {
                monitorTask = MessageFormat.Format(JGitText.Get().progressMonUploading, key);
            }
            string md5str = Base64.EncodeBytes(csum);
            long   len    = buf.Length();
            string lenstr = len.ToString();

            for (int curAttempt = 0; curAttempt < maxAttempts; curAttempt++)
            {
                HttpURLConnection c = Open("PUT", bucket, key);
                c.SetRequestProperty("Content-Length", lenstr);
                c.SetRequestProperty("Content-MD5", md5str);
                c.SetRequestProperty(X_AMZ_ACL, acl);
                encryption.Request(c, X_AMZ_META);
                Authorize(c);
                c.SetDoOutput(true);
                c.SetFixedLengthStreamingMode((int)len);
                monitor.BeginTask(monitorTask, (int)(len / 1024));
                OutputStream os = c.GetOutputStream();
                try
                {
                    buf.WriteTo(os, monitor);
                }
                finally
                {
                    monitor.EndTask();
                    os.Close();
                }
                switch (HttpSupport.Response(c))
                {
                case HttpURLConnection.HTTP_OK:
                {
                    return;
                }

                case HttpURLConnection.HTTP_INTERNAL_ERROR:
                {
                    continue;
                    goto default;
                }

                default:
                {
                    throw Error("Writing", key, c);
                }
                }
            }
            throw MaxAttempts("Writing", key);
        }