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)