{"record":{"id":"abbe633f3591c3bb","repo":"lionsoul2014/ip2region","slug":"setxdbinputstream-could-only-be-used-with-cachepol","errorCode":null,"errorMessage":"SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache","messagePattern":"SetXdbInputStream could ONLY be used with cachePolicy = Config\\.BufferCache","errorType":"validation","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"binding/java/src/main/java/org/lionsoul/ip2region/service/ConfigBuilder.java","lineNumber":93,"sourceCode":"    }\n\n    public ConfigBuilder setSearchers(int searchers) {\n        this.searchers = searchers;\n        return this;\n    }\n\n    public ConfigBuilder setFairLock(boolean fairLock) {\n        this.fairLock = fairLock;\n        return this;\n    }\n\n    private Config build(Version ipVersion) throws IOException, XdbException, InvalidConfigException {\n        if (xdbInputStream == null) {\n            // everyting is fine\n        } else if (cachePolicy != Config.BufferCache) {\n            // @Note: we can't directly rewrite the cachePolicy to Config.BufferCache.\n            // you must know what you are doing.\n            throw new InvalidConfigException(\"SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache\");\n        } else {\n            // 1, load the content buffer\n            final LongByteArray cBuffer = Searcher.loadContentFromInputStream(xdbInputStream, cacheSliceBytes);\n\n            // 2, load the header\n            final Header header = Searcher.loadHeaderFromBuffer(cBuffer);\n\n            // 3, verify the xdb from the buffer\n            Searcher.verify(header, cBuffer.length());\n\n            // create the config without xdbFile and vIndex\n            return new Config(cachePolicy, ipVersion, null, header, null, cBuffer, searchers, fairLock);\n        }\n\n        // load the header and the cache buffer\n        final File xdbFile;\n        if (this.xdbFile != null) {\n            xdbFile = this.xdbFile;","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/java/src/main/java/org/lionsoul/ip2region/service/ConfigBuilder.java#L75-L111","documentation":"ConfigBuilder.build() refuses to proceed when an xdb InputStream was set via setXdbInputStream but the cachePolicy is not Config.BufferCache. Streaming input can only be fully loaded into memory, so other cache policies (which require random access to a file) are incompatible. The library deliberately does not silently override the policy so the developer knows what they are doing.","triggerScenarios":"Calling ConfigBuilder.setXdbInputStream(...) (or its fluent variant) and then build()/asV4()/asV6() while cachePolicy is Config.VectorIndex or Config.Content (any policy other than BufferCache).","commonSituations":"Loading an xdb from a classpath resource, URL or non-file stream while copying a cachePolicy from file-based sample code; switching from file to stream input without removing the old policy.","solutions":["Set the cache policy to Config.BufferCache via ConfigBuilder.cachePolicy(Config.BufferCache) when using setXdbInputStream","Remove the setXdbInputStream call and instead use setXdbFile/setXdbPath if you need vector-index or other file-backed policies","If you must keep the stream, read the stream content yourself and pass the resulting byte buffer instead"],"exampleFix":"// before\nbuilder.setXdbInputStream(is).cachePolicy(Config.VectorIndex).asV4();\n// after\nbuilder.setXdbInputStream(is).cachePolicy(Config.BufferCache).asV4();","handlingStrategy":"validation","validationCode":"if (usingInputStream && cachePolicy != Config.BufferCache) {\n    throw new IllegalStateException(\"setXdbInputStream requires cachePolicy=Config.BufferCache\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ip2Region = new ConfigBuilder().setXdbInputStream(is).cachePolicy(Config.BufferCache).asV4();\n} catch (InvalidConfigException e) {\n    log.error(\"stream init requires BufferCache policy\", e);\n}","preventionTips":["Always pair setXdbInputStream with cachePolicy(Config.BufferCache)","Use setXdbPath/setXdbFile when you need vector-index or other file-backed policies","Read the @Note in ConfigBuilder: the policy is not auto-corrected on purpose"],"tags":["java","configuration","cache-policy"],"backgroundTag":"incompatible-cache-policy","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}