提交 52b25711 编写于 作者: K Kohsuke Kawaguchi

Merge pull request #737

Conflicts:
	core/src/main/java/hudson/model/UpdateCenter.java
......@@ -137,23 +137,27 @@ public class Main {
FileOutputStream os = new FileOutputStream(tmpFile);
Writer w = new OutputStreamWriter(os,"UTF-8");
w.write("<?xml version='1.0' encoding='UTF-8'?>");
w.write("<run><log encoding='hexBinary' content-encoding='"+Charset.defaultCharset().name()+"'>");
w.flush();
// run the command
long start = System.currentTimeMillis();
List<String> cmd = new ArrayList<String>();
for( int i=1; i<args.length; i++ )
cmd.add(args[i]);
Proc proc = new Proc.LocalProc(cmd.toArray(new String[0]),(String[])null,System.in,
new DualOutputStream(System.out,new EncodingStream(os)));
int ret = proc.join();
w.write("</log><result>"+ret+"</result><duration>"+(System.currentTimeMillis()-start)+"</duration></run>");
w.close();
int ret;
try {
w.write("<?xml version='1.0' encoding='UTF-8'?>");
w.write("<run><log encoding='hexBinary' content-encoding='"+Charset.defaultCharset().name()+"'>");
w.flush();
// run the command
long start = System.currentTimeMillis();
List<String> cmd = new ArrayList<String>();
for( int i=1; i<args.length; i++ )
cmd.add(args[i]);
Proc proc = new Proc.LocalProc(cmd.toArray(new String[0]),(String[])null,System.in,
new DualOutputStream(System.out,new EncodingStream(os)));
ret = proc.join();
w.write("</log><result>"+ret+"</result><duration>"+(System.currentTimeMillis()-start)+"</duration></run>");
} finally {
IOUtils.closeQuietly(w);
}
URL location = new URL(jobURL, "postBuildResult");
while(true) {
......@@ -170,8 +174,11 @@ public class Main {
con.connect();
// send the data
FileInputStream in = new FileInputStream(tmpFile);
Util.copyStream(in,con.getOutputStream());
in.close();
try {
Util.copyStream(in,con.getOutputStream());
} finally {
IOUtils.closeQuietly(in);
}
if(con.getResponseCode()!=200) {
Util.copyStream(con.getErrorStream(),System.err);
......
......@@ -171,16 +171,22 @@ public abstract class ConsoleNote<T> implements Serializable, Describable<Consol
private ByteArrayOutputStream encodeToBytes() throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(buf));
oos.writeObject(this);
oos.close();
try {
oos.writeObject(this);
} finally {
oos.close();
}
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2,true,-1,null));
buf2.write(PREAMBLE);
dos.writeInt(buf.size());
buf.writeTo(dos);
dos.close();
try {
buf2.write(PREAMBLE);
dos.writeInt(buf.size());
buf.writeTo(dos);
} finally {
dos.close();
}
buf2.write(POSTAMBLE);
return buf2;
}
......
......@@ -111,8 +111,11 @@ public class WindowsServiceLifecycle extends Lifecycle {
File copyFiles = new File(rootDir,baseName+".copies");
FileWriter w = new FileWriter(copyFiles, true);
w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n');
w.close();
try {
w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n');
} finally {
w.close();
}
}
@Override
......
......@@ -197,12 +197,16 @@ public class FileParameterValue extends ParameterValue {
File fileParameter = getLocationUnderBuild(build);
if (fileParameter.isFile()) {
InputStream data = new FileInputStream(fileParameter);
long lastModified = fileParameter.lastModified();
long contentLength = fileParameter.length();
if (request.hasParameter("view")) {
response.serveFile(request, data, lastModified, contentLength, "plain.txt");
} else {
response.serveFile(request, data, lastModified, contentLength, originalFileName);
try {
long lastModified = fileParameter.lastModified();
long contentLength = fileParameter.length();
if (request.hasParameter("view")) {
response.serveFile(request, data, lastModified, contentLength, "plain.txt");
} else {
response.serveFile(request, data, lastModified, contentLength, originalFileName);
}
} finally {
IOUtils.closeQuietly(data);
}
}
}
......
......@@ -2082,8 +2082,9 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
} finally {
IOUtils.closeQuietly(input);
}
}
out.close();
} finally {
IOUtils.closeQuietly(out);
}
}
/**
......
......@@ -45,6 +45,8 @@ import hudson.util.DaemonThreadFactory;
import hudson.util.FormValidation;
import hudson.util.HttpResponses;
import hudson.util.NamingThreadFactory;
import hudson.util.IOException2;
import hudson.util.IOUtils;
import hudson.util.PersistedList;
import hudson.util.XStream2;
import jenkins.RestartRequiredException;
......@@ -745,16 +747,18 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas
* @see DownloadJob
*/
public File download(DownloadJob job, URL src) throws IOException {
CountingInputStream in = null;
OutputStream out = null;
try {
URLConnection con = connect(job,src);
int total = con.getContentLength();
CountingInputStream in = new CountingInputStream(con.getInputStream());
in = new CountingInputStream(con.getInputStream());
byte[] buf = new byte[8192];
int len;
File dst = job.getDestination();
File tmp = new File(dst.getPath()+".tmp");
OutputStream out = new FileOutputStream(tmp);
out = new FileOutputStream(tmp);
LOGGER.info("Downloading "+job.getName());
try {
......@@ -766,9 +770,6 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas
throw new IOException("Failed to load "+src+" to "+tmp,e);
}
in.close();
out.close();
if (total!=-1 && total!=tmp.length()) {
// don't know exactly how this happens, but report like
// http://www.ashlux.com/wordpress/2009/08/14/hudson-and-the-sonar-plugin-fail-maveninstallation-nosuchmethoderror/
......@@ -778,7 +779,10 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas
return tmp;
} catch (IOException e) {
throw new IOException("Failed to download from "+src,e);
throw new IOException2("Failed to download from "+src,e);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
......
......@@ -28,6 +28,7 @@ import hudson.PluginWrapper;
import hudson.Util;
import hudson.Extension;
import hudson.node_monitors.ArchitectureMonitor.DescriptorImpl;
import hudson.util.IOUtils;
import hudson.util.Secret;
import static hudson.util.TimeUnit2.DAYS;
......@@ -172,8 +173,11 @@ public class UsageStatistics extends PageDecorator {
// json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string
OutputStreamWriter w = new OutputStreamWriter(new GZIPOutputStream(new CombinedCipherOutputStream(baos,getKey(),"AES")), "UTF-8");
o.write(w);
w.close();
try {
o.write(w);
} finally {
IOUtils.closeQuietly(w);
}
return new String(Base64.encode(baos.toByteArray()));
} catch (GeneralSecurityException e) {
......
......@@ -41,6 +41,7 @@ import hudson.model.Describable;
import hudson.model.TaskListener;
import hudson.model.Node;
import hudson.model.WorkspaceCleanupThread;
import hudson.util.IOUtils;
import jenkins.model.Jenkins;
import hudson.model.Descriptor;
import hudson.model.Api;
......@@ -578,14 +579,17 @@ public abstract class SCM implements Describable<SCM>, ExtensionPoint {
//
protected final boolean createEmptyChangeLog(File changelogFile, BuildListener listener, String rootTag) {
FileWriter w = null;
try {
FileWriter w = new FileWriter(changelogFile);
w = new FileWriter(changelogFile);
w.write("<"+rootTag +"/>");
w.close();
return true;
} catch (IOException e) {
e.printStackTrace(listener.error(e.getMessage()));
return false;
} finally {
IOUtils.closeQuietly(w);
}
}
......
......@@ -178,8 +178,11 @@ public class JDKInstaller extends ToolInstaller {
byte[] header = new byte[2];
{
DataInputStream in = new DataInputStream(fs.read(jdkBundle));
in.readFully(header);
in.close();
try {
in.readFully(header);
} finally {
IOUtils.closeQuietly(in);
}
}
ProcStarter starter;
......
......@@ -31,6 +31,7 @@ import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Cause;
import hudson.util.IOUtils;
import jenkins.model.Jenkins;
import hudson.model.Item;
import hudson.model.Project;
......@@ -337,8 +338,11 @@ public class SCMTrigger extends Trigger<SCMedItem> {
rsp.setContentType("text/plain;charset=UTF-8");
// Prevent jelly from flushing stream so Content-Length header can be added afterwards
FlushProofOutputStream out = new FlushProofOutputStream(rsp.getCompressedOutputStream(req));
getPollingLogText().writeLogTo(0, out);
out.close();
try {
getPollingLogText().writeLogTo(0, out);
} finally {
IOUtils.closeQuietly(out);
}
}
public AnnotatedLargeText getPollingLogText() {
......
......@@ -115,11 +115,14 @@ public class CompressedFile {
StringBuilder str = new StringBuilder((int)sizeGuess);
Reader r = new InputStreamReader(read());
char[] buf = new char[8192];
int len;
while((len=r.read(buf,0,buf.length))>0)
str.append(buf,0,len);
r.close();
try {
char[] buf = new char[8192];
int len;
while((len=r.read(buf,0,buf.length))>0)
str.append(buf,0,len);
} finally {
IOUtils.closeQuietly(r);
}
return str.toString();
}
......
......@@ -70,10 +70,13 @@ final class ZipArchiver extends Archiver {
fileZipEntry.setTime(f.lastModified());
zip.putNextEntry(fileZipEntry);
FileInputStream in = new FileInputStream(f);
int len;
while((len=in.read(buf))>=0)
zip.write(buf,0,len);
in.close();
try {
int len;
while((len=in.read(buf))>=0)
zip.write(buf,0,len);
} finally {
in.close();
}
zip.closeEntry();
}
entriesWritten++;
......
......@@ -172,6 +172,7 @@ import hudson.util.FormValidation;
import hudson.util.Futures;
import hudson.util.HudsonIsLoading;
import hudson.util.HudsonIsRestarting;
import hudson.util.IOUtils;
import hudson.util.Iterators;
import hudson.util.JenkinsReloadFailed;
import hudson.util.Memoizer;
......@@ -3981,12 +3982,15 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
private static void computeVersion(ServletContext context) {
// set the version
Properties props = new Properties();
InputStream is = null;
try {
InputStream is = Jenkins.class.getResourceAsStream("jenkins-version.properties");
is = Jenkins.class.getResourceAsStream("jenkins-version.properties");
if(is!=null)
props.load(is);
} catch (IOException e) {
e.printStackTrace(); // if the version properties is missing, that's OK.
} finally {
IOUtils.closeQuietly(is);
}
String ver = props.getProperty("version");
if(ver==null) ver="?";
......
......@@ -65,13 +65,13 @@ public class ZipArchiverTest extends TestCase {
} catch (Exception e) {
fail("exception driving ZipArchiver", e);
} finally {
try {
archiver.close();
} catch (IOException e) {
// ignored
if (archiver != null) {
try {
archiver.close();
} catch (IOException e) {
// ignored
}
}
archiver = null;
}
// examine zip contents and assert that none of the entry names (paths) have
......@@ -86,10 +86,12 @@ public class ZipArchiverTest extends TestCase {
} catch (Exception e) {
fail("failure enumerating zip entries", e);
} finally {
try {
zipFileVerify.close();
} catch (IOException e) {
// ignored
if (zipFileVerify != null) {
try {
zipFileVerify.close();
} catch (IOException e) {
// ignored
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册