Search code examples
javainputstream

The method readAllBytes() is undefined for the type InputStream


I cannot get rid of The method readAllBytes() is undefined for the type InputStream error. I assume this caused because of the version I use ? JDK compliance 1.8 Please advise thank you.

if (http.getResponseCode() == 200) {                    
            ObjectMapper mapper = new ObjectMapper();           
            try (InputStream inputStream = http.getInputStream()) {
                String jsonContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
                JsonNode node = mapper.readTree(jsonContent);
                String Stytch_user_id = node.get("user_id").textValue();
                Object[] objects = getUserLogin(Stytch_user_id, "P0002","123456789");
            }       
        } else { 
            System.out.println("failed");
        }

Solution

  • Use the readTree(Reader r) overloaded method to let Jackson read the stream for you:

    JsonNode node;
    try (Reader in = new InputStreamReader(http.getInputStream(), StandardCharsets.UTF_8)) {
        ObjectMapper mapper = new ObjectMapper();
        node = mapper.readTree(in);
    }
    
    String Stytch_user_id = node.get("user_id").textValue();
    Object[] objects = getUserLogin(Stytch_user_id, "P0002","123456789");