Program: 47056
Project: 47056
Buggy source:
/** 
 * Parses the old RealAudio 1.0 and 2.0 formats that's not supported by neither   {@link org.jaudiotagger} nor MediaInfo. Returns {@code false} if{@code channel} isn't one of these formats or the parsing fails.<p> Primary references: <ul> <li><a href="https://wiki.multimedia.cx/index.php/RealMedia">RealAudio on MultimediaWiki</a></li> <li><a href="https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/rmdec.c" >FFmpeg rmdec.c</a></li> </ul>
 * @param channel the {@link Channel} containing the input. Size will onlybe parsed if  {@code channel} is a {@link FileChannel}instance.
 * @param media the {@link DLNAMediaInfo} instance to write the parsingresults to.
 * @return {@code true} if the {@code channel} input is in RealAudio 1.0 or2.0 format and the parsing succeeds; false otherwise
 */
public static boolean parseRealAudio(ReadableByteChannel channel,DLNAMediaInfo media){
  final byte[] magicBytes={0x2E,0x72,0x61,(byte)0xFD};
  ByteBuffer buffer=ByteBuffer.allocate(8);
  buffer.order(ByteOrder.BIG_ENDIAN);
  DLNAMediaAudio audio=new DLNAMediaAudio();
  try {
    int count=channel.read(buffer);
    if (count < 4) {
      LOGGER.trace("String_Node_Str");
      return false;
    }
    buffer.flip();
    byte[] signature=new byte[4];
    buffer.get(signature);
    if (!Arrays.equals(magicBytes,signature)) {
      if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("String_Node_Str",new String(signature,StandardCharsets.US_ASCII));
      }
      return false;
    }
    media.setContainer(FormatConfiguration.RA);
    short version=buffer.getShort();
    int reportedHeaderSize=0;
    int reportedDataSize=0;
    if (version == 3) {
      audio.setCodecA("String_Node_Str");
      audio.getAudioProperties().setNumberOfChannels(1);
      audio.getAudioProperties().setSampleFrequency(8000);
      short headerSize=buffer.getShort();
      buffer=ByteBuffer.allocate(headerSize);
      channel.read(buffer);
      buffer.flip();
      buffer.position(8);
      int bytesPerMinute=buffer.getShort() & 0xFFFF;
      reportedDataSize=buffer.getInt();
      byte b=buffer.get();
      if (b != 0) {
        byte[] title=new byte[b & 0xFF];
        buffer.get(title);
        String titleString=new String(title,StandardCharsets.US_ASCII);
        audio.setSongname(titleString);
        audio.setAudioTrackTitleFromMetadata(titleString);
      }
      b=buffer.get();
      if (b != 0) {
        byte[] artist=new byte[b & 0xFF];
        buffer.get(artist);
        audio.setArtist(new String(artist,StandardCharsets.US_ASCII));
      }
      audio.setBitRate(bytesPerMinute * 8 / 60);
      media.setBitrate(bytesPerMinute * 8 / 60);
    }
 else     if (version == 4 || version == 5) {
      buffer=ByteBuffer.allocate(14);
      channel.read(buffer);
      buffer.flip();
      buffer.get(signature);
      if (!"String_Node_Str".equals(new String(signature,StandardCharsets.US_ASCII))) {
        LOGGER.debug("String_Node_Str",new String(signature,StandardCharsets.US_ASCII));
        return false;
      }
      reportedDataSize=buffer.getInt();
      buffer.getShort();
      reportedHeaderSize=buffer.getInt();
      buffer=ByteBuffer.allocate(reportedHeaderSize);
      channel.read(buffer);
      buffer.flip();
      buffer.getShort();
      buffer.getInt();
      buffer.getInt();
      long bytesPerMinute=buffer.getInt() & 0xFFFFFFFFL;
      buffer.getInt();
      buffer.getShort();
      buffer.getShort();
      buffer.getShort();
      buffer.getShort();
      if (version == 5) {
        buffer.position(buffer.position() + 6);
      }
      short sampleRate=buffer.getShort();
      buffer.getShort();
      short sampleSize=buffer.getShort();
      short nrChannels=buffer.getShort();
      byte[] fourCC;
      if (version == 4) {
        buffer.position(buffer.get() + buffer.position());
        fourCC=new byte[buffer.get()];
        buffer.get(fourCC);
      }
 else {
        buffer.getFloat();
        fourCC=new byte[4];
        buffer.get(fourCC);
      }
      String fourCCString=new String(fourCC,StandardCharsets.US_ASCII).toLowerCase(Locale.ROOT);
switch (fourCCString) {
case "String_Node_Str":
        audio.setCodecA("String_Node_Str");
      break;
case "String_Node_Str":
    audio.setCodecA("String_Node_Str");
  break;
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.AC3);
break;
case "String_Node_Str":
audio.setCodecA("String_Node_Str");
break;
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.COOK);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.ATRAC);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.RALF);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.AAC_LC);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.HE_AAC);
default :
LOGGER.debug("String_Node_Str",fourCCString);
return false;
}
if (buffer.hasRemaining()) {
parseRealAudioMetaData(buffer,audio,version);
}
audio.setBitRate((int)(bytesPerMinute * 8 / 60));
media.setBitrate((int)(bytesPerMinute * 8 / 60));
audio.setBitsperSample(sampleSize);
audio.getAudioProperties().setNumberOfChannels(nrChannels);
audio.getAudioProperties().setSampleFrequency(sampleRate);
}
 else {
LOGGER.error("String_Node_Str",version);
return false;
}
media.getAudioTracksList().add(audio);
long fileSize=0;
if (channel instanceof FileChannel) {
fileSize=((FileChannel)channel).size();
media.setSize(fileSize);
}
if (audio.getBitRate() > 0) {
int dataSize;
if (fileSize > 0 && reportedHeaderSize > 0) {
int fullHeaderSize=reportedHeaderSize + (version == 3 ? 8 : 16);
if (reportedDataSize > 0) {
dataSize=(int)Math.min(reportedDataSize,fileSize - fullHeaderSize);
}
 else {
dataSize=(int)(fileSize - fullHeaderSize);
}
}
 else {
dataSize=reportedDataSize;
}
media.setDuration((double)dataSize / audio.getBitRate() * 8);
}
}
 catch (IOException e) {
LOGGER.debug("String_Node_Str",e.getMessage());
LOGGER.trace("String_Node_Str",e);
return false;
}
if (PMS.getConfiguration() != null && !PMS.getConfiguration().getAudioThumbnailMethod().equals(CoverSupplier.NONE) && (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname()) || StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist()))) {
ID3v1Tag tag=new ID3v1Tag();
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname())) {
tag.setTitle(media.getFirstAudioTrack().getSongname());
}
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist())) {
tag.setArtist(media.getFirstAudioTrack().getArtist());
}
try {
media.setThumb(DLNAThumbnail.toThumbnail(CoverUtil.get().getThumbnail(tag),640,480,ScaleType.MAX,ImageFormat.SOURCE,false));
}
 catch (IOException e) {
LOGGER.error("String_Node_Str",tag.getFirstTitle(),tag.getFirstArtist());
}
}
media.setThumbready(true);
return true;
}

Fixed version (ground truth):
/** 
 * Parses the old RealAudio 1.0 and 2.0 formats that's not supported by neither   {@link org.jaudiotagger} nor MediaInfo. Returns {@code false} if{@code channel} isn't one of these formats or the parsing fails.<p> Primary references: <ul> <li><a href="https://wiki.multimedia.cx/index.php/RealMedia">RealAudio on MultimediaWiki</a></li> <li><a href="https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/rmdec.c" >FFmpeg rmdec.c</a></li> </ul>
 * @param channel the {@link Channel} containing the input. Size will onlybe parsed if  {@code channel} is a {@link FileChannel}instance.
 * @param media the {@link DLNAMediaInfo} instance to write the parsingresults to.
 * @return {@code true} if the {@code channel} input is in RealAudio 1.0 or2.0 format and the parsing succeeds; false otherwise
 */
public static boolean parseRealAudio(ReadableByteChannel channel,DLNAMediaInfo media){
  final byte[] magicBytes={0x2E,0x72,0x61,(byte)0xFD};
  ByteBuffer buffer=ByteBuffer.allocate(8);
  buffer.order(ByteOrder.BIG_ENDIAN);
  DLNAMediaAudio audio=new DLNAMediaAudio();
  try {
    int count=channel.read(buffer);
    if (count < 4) {
      LOGGER.trace("String_Node_Str");
      return false;
    }
    buffer.flip();
    byte[] signature=new byte[4];
    buffer.get(signature);
    if (!Arrays.equals(magicBytes,signature)) {
      if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("String_Node_Str",new String(signature,StandardCharsets.US_ASCII));
      }
      return false;
    }
    media.setContainer(FormatConfiguration.RA);
    short version=buffer.getShort();
    int reportedHeaderSize=0;
    int reportedDataSize=0;
    if (version == 3) {
      audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
      audio.getAudioProperties().setNumberOfChannels(1);
      audio.getAudioProperties().setSampleFrequency(8000);
      short headerSize=buffer.getShort();
      buffer=ByteBuffer.allocate(headerSize);
      channel.read(buffer);
      buffer.flip();
      buffer.position(8);
      int bytesPerMinute=buffer.getShort() & 0xFFFF;
      reportedDataSize=buffer.getInt();
      byte b=buffer.get();
      if (b != 0) {
        byte[] title=new byte[b & 0xFF];
        buffer.get(title);
        String titleString=new String(title,StandardCharsets.US_ASCII);
        audio.setSongname(titleString);
        audio.setAudioTrackTitleFromMetadata(titleString);
      }
      b=buffer.get();
      if (b != 0) {
        byte[] artist=new byte[b & 0xFF];
        buffer.get(artist);
        audio.setArtist(new String(artist,StandardCharsets.US_ASCII));
      }
      audio.setBitRate(bytesPerMinute * 8 / 60);
      media.setBitrate(bytesPerMinute * 8 / 60);
    }
 else     if (version == 4 || version == 5) {
      buffer=ByteBuffer.allocate(14);
      channel.read(buffer);
      buffer.flip();
      buffer.get(signature);
      if (!"String_Node_Str".equals(new String(signature,StandardCharsets.US_ASCII))) {
        LOGGER.debug("String_Node_Str",new String(signature,StandardCharsets.US_ASCII));
        return false;
      }
      reportedDataSize=buffer.getInt();
      buffer.getShort();
      reportedHeaderSize=buffer.getInt();
      buffer=ByteBuffer.allocate(reportedHeaderSize);
      channel.read(buffer);
      buffer.flip();
      buffer.getShort();
      buffer.getInt();
      buffer.getInt();
      long bytesPerMinute=buffer.getInt() & 0xFFFFFFFFL;
      buffer.getInt();
      buffer.getShort();
      buffer.getShort();
      buffer.getShort();
      buffer.getShort();
      if (version == 5) {
        buffer.position(buffer.position() + 6);
      }
      short sampleRate=buffer.getShort();
      buffer.getShort();
      short sampleSize=buffer.getShort();
      short nrChannels=buffer.getShort();
      byte[] fourCC;
      if (version == 4) {
        buffer.position(buffer.get() + buffer.position());
        fourCC=new byte[buffer.get()];
        buffer.get(fourCC);
      }
 else {
        buffer.getFloat();
        fourCC=new byte[4];
        buffer.get(fourCC);
      }
      String fourCCString=new String(fourCC,StandardCharsets.US_ASCII).toLowerCase(Locale.ROOT);
switch (fourCCString) {
case "String_Node_Str":
        audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
      break;
case "String_Node_Str":
    audio.setCodecA(FormatConfiguration.REALAUDIO_28_8);
  break;
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.AC3);
break;
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.SIPRO);
break;
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.COOK);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.ATRAC);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.RALF);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.AAC_LC);
case "String_Node_Str":
audio.setCodecA(FormatConfiguration.HE_AAC);
default :
LOGGER.debug("String_Node_Str",fourCCString);
return false;
}
if (buffer.hasRemaining()) {
parseRealAudioMetaData(buffer,audio,version);
}
audio.setBitRate((int)(bytesPerMinute * 8 / 60));
media.setBitrate((int)(bytesPerMinute * 8 / 60));
audio.setBitsperSample(sampleSize);
audio.getAudioProperties().setNumberOfChannels(nrChannels);
audio.getAudioProperties().setSampleFrequency(sampleRate);
}
 else {
LOGGER.error("String_Node_Str",version);
return false;
}
media.getAudioTracksList().add(audio);
long fileSize=0;
if (channel instanceof FileChannel) {
fileSize=((FileChannel)channel).size();
media.setSize(fileSize);
}
if (audio.getBitRate() > 0) {
int dataSize;
if (fileSize > 0 && reportedHeaderSize > 0) {
int fullHeaderSize=reportedHeaderSize + (version == 3 ? 8 : 16);
if (reportedDataSize > 0) {
dataSize=(int)Math.min(reportedDataSize,fileSize - fullHeaderSize);
}
 else {
dataSize=(int)(fileSize - fullHeaderSize);
}
}
 else {
dataSize=reportedDataSize;
}
media.setDuration((double)dataSize / audio.getBitRate() * 8);
}
}
 catch (IOException e) {
LOGGER.debug("String_Node_Str",e.getMessage());
LOGGER.trace("String_Node_Str",e);
return false;
}
if (PMS.getConfiguration() != null && !PMS.getConfiguration().getAudioThumbnailMethod().equals(CoverSupplier.NONE) && (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname()) || StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist()))) {
ID3v1Tag tag=new ID3v1Tag();
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname())) {
tag.setTitle(media.getFirstAudioTrack().getSongname());
}
if (StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist())) {
tag.setArtist(media.getFirstAudioTrack().getArtist());
}
try {
media.setThumb(DLNAThumbnail.toThumbnail(CoverUtil.get().getThumbnail(tag),640,480,ScaleType.MAX,ImageFormat.SOURCE,false));
}
 catch (IOException e) {
LOGGER.error("String_Node_Str",tag.getFirstTitle(),tag.getFirstArtist());
}
}
media.setThumbready(true);
media.setMediaparsed(true);
return true;
}