Clean out a few warnings on potential resource leakage

This commit is contained in:
Hugo Trippaers 2013-09-21 15:32:52 +08:00
parent d37b87d97a
commit 8b6dcf8b6e
2 changed files with 38 additions and 3 deletions

View File

@ -199,25 +199,29 @@ public class NumbersUtil {
public static String toReadableSize(long bytes) {
if (bytes <= KB && bytes >= 0) {
return Long.toString(bytes) + " bytes";
} else if (bytes <= MB) {
} else if (bytes < MB) {
StringBuilder builder = new StringBuilder();
Formatter format = new Formatter(builder);
format.format("%.2f KB", (float)bytes / (float)KB);
format.close();
return builder.toString();
} else if (bytes <= GB) {
} else if (bytes < GB) {
StringBuilder builder = new StringBuilder();
Formatter format = new Formatter(builder);
format.format("%.2f MB", (float)bytes / (float)MB);
format.close();
return builder.toString();
} else if (bytes <= TB) {
} else if (bytes < TB) {
StringBuilder builder = new StringBuilder();
Formatter format = new Formatter(builder);
format.format("%.2f GB", (float)bytes / (float)GB);
format.close();
return builder.toString();
} else {
StringBuilder builder = new StringBuilder();
Formatter format = new Formatter(builder);
format.format("%.4f TB", (float)bytes / (float)TB);
format.close();
return builder.toString();
}
}

View File

@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils;
import static org.junit.Assert.*;
import org.junit.Test;
public class NumbersUtilTest {
@Test
public void formattingCheck() {
long size = 1024*1024*1024;
String formatted = NumbersUtil.toReadableSize(size);
assertEquals("1.00 GB", formatted);
}
}