void (@NotNull VirtualFile virtualFile) { byte[] bom = virtualFile.getBOM(); if (bom != null) return; Charset charset = virtualFile.getCharset(); byte[] possibleBom = CharsetToolkit.getPossibleBom(charset); if (possibleBom == null) return; virtualFile.setBOM(possibleBom); try { byte[] bytes = virtualFile.contentsToByteArray(); byte[] contentWithAddedBom = ArrayUtil.mergeArrays(possibleBom, bytes); WriteAction.runAndWait(() -> virtualFile.setBinaryContent(contentWithAddedBom)); } catch (IOException ex) { LOG.warn("Unexpected exception occurred in file " + virtualFile, ex); } }