Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:19:24+00:00 2026-06-12T19:19:24+00:00

When calling .length() on a file object, files over 2 GB return an incorrect

  • 0

When calling .length() on a file object, files over 2 GB return an incorrect value.

This is happening in a web application running in a tomcat container.

For example, a file that windows reports as 2,083,344,714 bytes is coming back as 1887961088 from java.

Here are the environmental details:

  • jdk 1.6.0_24(64 bit)
  • Java JotSpot(TM) 64-Bit Server VM (build 17.0-b17, mixed mode)
  • Tomcat 6.0.29
  • Windows Server 2008 r2

So I immediately suspected something to do with a 32bit VM, but everything appears to be running in 64 bit. I’m totally stumped, any help would be greatly appreciated, if only help developing some tests to see if the problem is related to something running in 32 mode somehow..

EDIT:

To answer some comments:
When I run a really simple test case using the same JVM (just output file.length) I get the correct value: 2,083,344,714.

I am pretty confident that this should work, I am just feeling like something about my tomcat configuration/ java configuration/ application is giving me a hard time. The fact that it is affecting only files over a certain size really makes it seem like an issue with 32 bit vs 64 bit, but as far as I can tell everything is 64 bit.

EDIT 2: Starting to suspect this is pretty much not possible, and I might have an issue with a thread taking the length of a file before it has been copied completely. I’ll post here when I figure it out, if anyone cares 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:19:25+00:00Added an answer on June 12, 2026 at 7:19 pm

    Seems to work fine for me…

    pagefile.sys on Windows 7 x64, reported by DOS as 8446545920 bytes

    Java 7.0_02 x32
    8446545920 / 7.87 gb

    Java 1.6_30 x32
    8446545920 / 7.87 gb

    import java.io.File;
    import java.text.NumberFormat;
    
    public class TestFileSize {
    
        public static void main(String[] args) {
            File test1 = new File("C:/pagefile.sys");
            File test2 = new File("C:/hiberfil.sys");
    
            System.out.println(test1.length() + " / " + ByteFormatter.format(test1.length()));
            System.out.println(test2.length() + " / " + ByteFormatter.format(test2.length()));
    
        }
    
        public static class ByteFormatter {
    
            public static final long KILO_BYTES = 1024;
            public static final long MEGA_BYTES = 1024 * KILO_BYTES;
            public static final long GIGA_BYTES = 1024 * MEGA_BYTES;
            public static final long TERA_BYTES = 1024 * GIGA_BYTES;
            public enum Format {
    
                TeraBytes(TERA_BYTES),
                GigaBytes(GIGA_BYTES),
                MegaBytes(MEGA_BYTES),
                KiloBytes(KILO_BYTES);
                private long multiplier;
    
                private Format(long multiplier) {
                    this.multiplier = multiplier;
                }
    
                public long getMultiplier() {
                    return multiplier;
                }
            }
    
            public static String format(long bytes) {
                NumberFormat nf = NumberFormat.getNumberInstance();
                nf.setMaximumFractionDigits(2);
                nf.setMinimumFractionDigits(0);
                String format = bytes + " bytes";
                if (bytes / TERA_BYTES > 0) {
                    format = nf.format((double) bytes / TERA_BYTES) + " tb";
                } else if (bytes / GIGA_BYTES > 0) {
                    format = nf.format((double) bytes / GIGA_BYTES) + " gb";
                } else if (bytes / MEGA_BYTES > 0) {
                    format = nf.format((double) bytes / MEGA_BYTES) + " mb";
                } else if (bytes / KILO_BYTES > 0) {
                    format = nf.format((double) bytes / KILO_BYTES) + " kb";
                } else {
                    format = nf.format(bytes) + " bytes";
                }
    
                return format;
            }
    
            public static String formatMegaBytes(long lMegaBytes) {
    
                return format((long) ((double) lMegaBytes * MEGA_BYTES));
    
            }
    
            public static String formatKiloBytes(long kbytes) {
    
                return format(kbytes * KILO_BYTES);
    
            }
    
            public static String formatGigaBytes(long gbytes) {
    
                return format(gbytes * GIGA_BYTES);
    
            }
    
            public static String formatTeraBytes(long gbytes) {
    
                return format(gbytes * TERA_BYTES);
    
            }
    
            public static long toBytes(String value, Format format) {
    
                long multipler = format.getMultiplier();
    
                long bytes = (long) (Double.parseDouble(value.trim()) * multipler);
    
                return bytes;
    
            }
    
            public static long toBytes(String sValue) {
    
                long lBytes = 0;
    
                if (sValue.indexOf(" ") > -1) {
    
                    String sElements[] = sValue.split(" ");
    
                    lBytes = Long.parseLong(sElements[0]);
    
                    if (sElements[1].toLowerCase().startsWith("gb")) {
    
                        lBytes *= GIGA_BYTES;
    
                    } else if (sElements[1].toLowerCase().startsWith("mb")) {
    
                        lBytes *= MEGA_BYTES;
    
                    } else if (sElements[1].toLowerCase().startsWith("kb")) {
    
                        lBytes *= KILO_BYTES;
    
                    }
    
                } else {
    
                    sValue = sValue.trim();
                    long lMultiplier = 1;
                    String sBytes = null;
    
                    if (sValue.toLowerCase().endsWith("gb")) {
    
                        sBytes = sValue.substring(0, sValue.toLowerCase().indexOf("gb"));
                        lMultiplier = GIGA_BYTES;
    
                    } else if (sValue.toLowerCase().endsWith("mb")) {
    
                        sBytes = sValue.substring(0, sValue.toLowerCase().indexOf("mb"));
                        lMultiplier = MEGA_BYTES;
    
                    } else if (sValue.toLowerCase().endsWith("kb")) {
    
                        sBytes = sValue.substring(0, sValue.toLowerCase().indexOf("kb"));
                        lMultiplier = KILO_BYTES;
    
                    }
    
                    lBytes = Long.parseLong(sBytes);
    
                    lBytes *= lMultiplier;
    
                }
    
                return lBytes;
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so live docs says this for calling .lenght() on an XML object For XML
While calling from my Web Application, WCF response i am getting is : <s:Envelope
I am loading a php file and calling this function decodeJSON want to parse
Calling getActionBar returns null . This has been frequently reported so I've made sure
Why might my GetRawData declared function return a correct value when called from my
i am calling a batch file for xcopy. i have the path of the
This is my jquery code to upload multiple file. Input file generated dynamically so
My application accepts file uploads, with some metadata being stored in the DB, and
I am calling a method from C# like this: [DllImport(@C:\Hash.dll, CallingConvention = CallingConvention.Cdecl)] public
I have this massive nested loop scenario that is calling the DB and making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.