{"record":{"id":"c70c19dec2b45f4d","repo":"lionsoul2014/ip2region","slug":"incomplete-read-read-bytes-should-be-buff-lengt-c70c19","errorCode":null,"errorMessage":"incomplete read: read bytes should be ${buff.length}, got `${rLen}`","messagePattern":"incomplete read: read bytes should be (.+?), got `(.+?)`","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java","lineNumber":283,"sourceCode":"\n    // --- read xdb content\n\n    // -- load xdb buffer with random access file handle\n    \n    public static LongByteArray loadContent(RandomAccessFile handle) throws IOException {\n        return loadContent(handle, DEFAULT_SLICE_BYTES);\n    }\n\n    public static LongByteArray loadContent(RandomAccessFile handle, final int sliceBytes) throws IOException {\n        handle.seek(0);\n        // check the length and do the buff load\n        long toRead = handle.length();\n        final LongByteArray byteArray = new LongByteArray(sliceBytes);\n        while (toRead > 0) {\n            final byte[] buff = new byte[(int) Math.min(toRead, sliceBytes)];\n            final int rLen = handle.read(buff);\n            if (rLen != buff.length) {\n                throw new IOException(\"incomplete read: read bytes should be \" + buff.length + \", got `\" + rLen + \"`\");\n            }\n\n            byteArray.append(buff);\n            toRead -= rLen;\n        }\n\n        return byteArray;\n    }\n\n    // -- load xdb buffer with xdb file object\n\n    public static LongByteArray loadContentFromFile(File xdbFile) throws IOException {\n        return loadContentFromFile(xdbFile, DEFAULT_SLICE_BYTES);\n    }\n\n    public static LongByteArray loadContentFromFile(File xdbFile, final int sliceBytes) throws IOException {\n        final RandomAccessFile handle = new RandomAccessFile(xdbFile, \"r\");\n        final LongByteArray content = loadContent(handle, sliceBytes);","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java#L265-L301","documentation":"loadContent copies the entire xdb into a LongByteArray in slices; each handle.read must fill the whole slice. A short read (rLen != buff.length, including -1 at EOF) indicates the file ended prematurely or IO failed mid-copy, so an IOException with both expected and actual byte counts is thrown.","triggerScenarios":"Calling Searcher.loadContent (or the content cachePolicy constructor path) on a truncated xdb file, or a read error occurring mid-file on unstable storage.","commonSituations":"xdb download interrupted by network failure; container image built with an incompletely copied asset; disk full/IO faults on the host.","solutions":["Verify the xdb file size and checksum against the official release and replace the file if wrong","Run Searcher.verify on the file before loading content","Re-download the xdb with retry/atomic rename to avoid partial files","Check disk space and storage health if the failure is transient"],"exampleFix":"// before\nbyte[] buf = Searcher.loadContent(raf, 8192); // raf file truncated\n// after\nif (raf.length() < officialXdbSize) {\n    throw new IllegalStateException(\"xdb truncated, re-download\");\n}\nbyte[] buf = Searcher.loadContent(raf, 8192);","handlingStrategy":"validation","validationCode":"if (raf.length() < expectedXdbSize) {\n    throw new IllegalStateException(\"xdb truncated: \" + raf.length() + \" < \" + expectedXdbSize);\n}","typeGuard":null,"tryCatchPattern":"try {\n    byte[] content = Searcher.loadContent(raf, sliceBytes);\n} catch (IOException e) {\n    log.error(\"content load short-read; re-fetching xdb\", e);\n    content = redownloadAndLoad();\n}","preventionTips":["Pin and verify the xdb checksum in deployment pipelines","Use curl/wget with retries plus atomic rename for downloads","Monitor disk health/space on hosts serving the xdb"],"tags":["java","io","corrupt-file"],"backgroundTag":"incomplete-read","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}