0

enter image description here

I need to get serialized object from this table(it`s not a file or image. Just a table with columns in database which stored client with serialized data), but when i try to read Object i have an Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: EFBFBDEF. I know that when we have ObjectInputStream we need to have ObjectOutputStream. But I have data in table and not sure how to deserialize it properly.

Here my class

@Component

public class ClassicClientsService implements ClientDetailsService {

    private final JdbcTemplate dataTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public ClassicClientsService(DataSource dataSource) {
        this.dataTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {

        try {
            ClientDetails details = dataTemplate.queryForObject("SELECT SERIALIZATION FROM OAUTH_CLIENTS WHERE CLIENTID = ?", new ClientDetailsMapper(), new Object[]{clientId});
            return details;
        } catch (EmptyResultDataAccessException ers) {
            throw new EmptyResultDataAccessException("Client " + clientId + " was not found", 1);
        }

    }

    private class ClientDetailsMapper implements RowMapper<ClientDetails> {

        @SneakyThrows
        @Override
        public ClientDetails mapRow(ResultSet rs, int rowNum) {
            byte[] temp = rs.getBytes("SERIALIZATION");
            
            return (ClientDetails) new ObjectInputStream(new ByteArrayInputStream(temp)).readObject();
        }

    }}

public boolean addClient(ClientDetails details) throws DataAccessException {
        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
        try {
            ObjectOutputStream outObject = new ObjectOutputStream(outBytes);
            outObject.writeObject(details);
            outObject.flush();
            outObject.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] szDetails = outBytes.toByteArray();
        dataTemplate.update("INSERT INTO OAUTH_CLIENTS (CLIENTID, PROTOCOL, SERIALIZATION) VALUES(?,1,?)", new Object[]{details.getClientId(), szDetails});
        return false;
    }

When i call the method "loadClientByCLientId" i am getting my Exception

@Configuration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        migrateClients(args[0]);
    }

    private static void migrateClients(String clientId) {
        if (clientId.isEmpty()) {
            throw new RuntimeException("client id should be set as program argument");
        }
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        ClassicClientsService clientDetailsService = context.getBean(ClassicClientsService.class);
        OauthClientsManager oauthClientsManager = context.getBean(OauthClientsManager.class);
        clientDetailsService.loadClientByClientId(clientId);

Logs:


    NFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:935)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:374)
Feel free
  • 758
  • 5
  • 15
  • 1
    Is the file a text file? Is the image that you're showing an image of the text in the file? If so, then you cannot use an ObjectInputStream to extract data from the file. In fact, you can only use ObjectInputStream if the file was written using an ObjectOutputStream. Your question appears to be missing this key information. In other words, a file must contain properly serialized data if you are going to be able to unserialize it. – Hovercraft Full Of Eels Oct 21 '22 at 11:54
  • 1
    `EF BF BD EF BF BD 00 05` looks exactly like the sequence of [this answer](https://stackoverflow.com/a/10686702/2711488)… – Holger Oct 21 '22 at 12:09
  • @HovercraftFullOfEels It`s a column in database mysql. I have column 'SERIALIZATION' with serialized objects but i want to deserialize them and get data. – Feel free Oct 21 '22 at 12:50
  • But again, how is the data saved to the database? Is it serialized with ObjectOutputStream? If not, then your attempt to use ObjectInputStream is doomed. Regardless, you will want to show your code that is used when storing the data. – Hovercraft Full Of Eels Oct 21 '22 at 13:35
  • @HovercraftFullOfEels Sorry, forgot about 'addClient' method. If we use this method we will have serialized object with header AC ED 00 05. But if you can see i have other header because i used old databaseDump with patch which stored this data for me. Holger gave a link where answer was that git changes headers. But nobody answers how to read this header. Updated code and question. – Feel free Oct 21 '22 at 13:44
  • I could not reproduce your problem with [this code](https://pastebin.com/qjhccVDw). Consider trying to isolate the code that is causing the problem and posting that. – Hovercraft Full Of Eels Oct 21 '22 at 15:58
  • Are you by any chance trying to store that serialized data into a character-based column type? – g00se Oct 21 '22 at 18:42

0 Answers0