Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4680710

SourceDataLine.write() behavior is not correct for not open or not started lines

    Details

    • Subcomponent:
    • Resolved In Build:
      1.4.1
    • CPU:
      generic
    • OS:
      generic

      Description

      for not started line with incorrect length of data:");
                      writtenBytes = -1;
                      bufferSizeToWrite--;
                      try {
                          writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
                          out.println("> Bytes written: number of written bytes = " + writtenBytes);
                      } catch (IllegalArgumentException illegalArgumentException) {
                          out.println("> Permissible IllegalArgumentException for the present instance is thrown:");
                          illegalArgumentException.printStackTrace(out);
                      } catch (Throwable thrown) {
                          out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
                          out.println("# Unexpected Exception is thrown");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# SourceDataLine = " + testedSourceLine);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                      }
                      testedSourceLine.close();

                  } // for (int j=0; j < supportedSourceLineInfo.length; j++)
                  testedMixer.close();

              } // for (int i=0; i < installedMixersInfo.length; i++)

              if ( testResult == STATUS_FAILED ) {
                  out.println("\n==> test FAILED!");
              } else {
                  out.println("\n==> test PASSED!");
              }
              return testResult;
          }
          
      } // end of test class
      -------------------------------------------------------------------------
       
      ======================================================================


      Name: abR10010 Date: 05/07/2002




      The specification for the javax.sound.sampled.SourceDataLine interface doesn't describe
      behavior of the SourceDataLine.write(byte[] b, int off, int len) method for source lines
      which are not opened or not started yet.

      The test (see below) shows that such behavior of the write() method may be different
      for different cases.

      Sometimes the write() method method silently returns zero.

      Sometimes the write() method throws an IllegalArgumentException that would be correct
      if the tested line was opened and started.

      Sometimes the write() method throws a SecurityException which is not mentioned
      in the spec for this method at all but it is permissible when line is opened.

      It seems that such behavior of the write() method is not quite correct.

      May be an IllegalStateException is most suitable to the occasion when the
      write() method is called for source lines which are not opened or not started yet.

      Such behavior is shown by the test running with JDK 1.4.1-beta-b10.


      Please, see test log:

      % java -version
      java version "1.4.1-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b10)
      Java HotSpot(TM) Client VM (build 1.4.1-beta-b10, mixed mode)

      % java test

      ==> Test for SourceDataLine.write() method for not open and not started line:

      >>> testedMixer[0] = com.sun.media.sound.HeadspaceMixer@c2a132

      >> supportedSourceLineInfo.length = 2

      > testSourceLineInfo[0] = interface SourceDataLine supporting 8 audio formats
      > testSourceLine = com.sun.media.sound.MixerSourceLine@82c01f

      > check SourceDataLine.write() for not open line with correct length of data:
      > Bytes written: number of written bytes = 0

      > check SourceDataLine.write() for not open line with incorrect length of data:
      > Permissible IllegalArgumentException for the present instance is thrown:
      java.lang.IllegalArgumentException: Illegal request to write non-integral number of frames (3 bytes )
      at com.sun.media.sound.MixerSourceLine.write(MixerSourceLine.java:117)
      at test.run(test.java:154)
      at test.main(test.java:13)

      > open tested line:
      > OK - line is opened

      > check SourceDataLine.write() for not started line with correct length of data:
      > Bytes written: number of written bytes = 0

      > check SourceDataLine.write() for not started line with incorrect length of data:
      > Permissible IllegalArgumentException for the present instance is thrown:
      java.lang.IllegalArgumentException: Illegal request to write non-integral number of frames (3 bytes )
      at com.sun.media.sound.MixerSourceLine.write(MixerSourceLine.java:117)
      at test.run(test.java:212)
      at test.main(test.java:13)

      > testSourceLineInfo[1] = interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes
      > testSourceLine = com.sun.media.sound.MixerClip@1fdc96c
      > testSourceLine is not SourceDataLine

      >>> testedMixer[1] = com.sun.media.sound.SimpleInputDevice@b2fd8f

      >> supportedSourceLineInfo.length = 0

      >>> testedMixer[2] = com.sun.media.sound.SimpleOutputDevice@124bbbf

      >> supportedSourceLineInfo.length = 1

      > testSourceLineInfo[0] = interface SourceDataLine supporting 24 audio formats
      > testSourceLine = com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine@1e0bc08

      > check SourceDataLine.write() for not open line with correct length of data:
      ## SourceDataLine.write(byte[] b, int off, int len) failed:
      # Unexpected Exception is thrown
      # Mixer = com.sun.media.sound.SimpleOutputDevice@124bbbf
      # SourceDataLine = com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine@1e0bc08
      java.lang.SecurityException: Permission to read data from the output device not granted.
      at com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine.write(SimpleOutputDevice.java:468)
      at test.run(test.java:138)
      at test.main(test.java:13)

      > check SourceDataLine.write() for not open line with incorrect length of data:
      ## SourceDataLine.write(byte[] b, int off, int len) failed:
      # Unexpected Exception is thrown
      # Mixer = com.sun.media.sound.SimpleOutputDevice@124bbbf
      # SourceDataLine = com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine@1e0bc08
      java.lang.SecurityException: Permission to read data from the output device not granted.
      at com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine.write(SimpleOutputDevice.java:468)
      at test.run(test.java:154)
      at test.main(test.java:13)

      > open tested line:
      > Line is not available due to security restrictions:
      java.lang.SecurityException: Permission to read data from the output device not granted.
      at com.sun.media.sound.SimpleOutputDevice$OutputDeviceDataLine.implOpen(SimpleOutputDevice.java:486)
      at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:85)
      at test.run(test.java:172)
      at test.main(test.java:13)

      ==> test FAILED!
       
       
      The test source:
      ------------------------------- test.java --------------------------------
      // File: %Z%%M% %I% %E%
      // Copyright %G% Sun Microsystems, Inc. All Rights Reserved

      import javax.sound.sampled.*;

      public class test {

          static final int STATUS_PASSED = 0;
          static final int STATUS_FAILED = 2;
          static final int STATUS_TEMP = 95;
          
          public static void main(String argv[]) {
              int testExitStatus = run(argv, System.out) + STATUS_TEMP;
              System.exit(testExitStatus);
          }

          public static int run(String argv[], java.io.PrintStream out) {
              int testResult = STATUS_PASSED;
          
              out.println
                  ("\n==> Test for SourceDataLine.write() method for not open and not started line:");

              Mixer.Info[] installedMixersInfo = AudioSystem.getMixerInfo();
              
              if ( installedMixersInfo == null ) {
                  out.println("## AudioSystem.getMixerInfo() returned unexpected result:");
                  out.println("# expected: an array of Mixer.Info objects (may be array of length 0);");
                  out.println("# produced: null;");
                  return STATUS_FAILED;
              }

              if ( installedMixersInfo.length == 0 ) {
                  // there are no mixers installed on the system - so this testcase can not be tested
                  return STATUS_PASSED;
              }

              Mixer testedMixer = null;
              for (int i=0; i < installedMixersInfo.length; i++) {
                  try {
                      testedMixer = AudioSystem.getMixer(installedMixersInfo[i]);
                  } catch (SecurityException securityException) {
                      // installed Mixer is unavailable because of security restrictions
                      continue;
                  } catch (Throwable thrown) {
                      out.println("## AudioSystem.getMixer() threw unexpected exception:");
                      thrown.printStackTrace(out);
                      testResult = STATUS_FAILED;
                      continue;
                  }

                  try {
                      testedMixer.open();
                  } catch (LineUnavailableException lineUnavailableException) {
                      // testedMixer is not available due to resource restrictions
                      continue;
                  } catch (SecurityException securityException) {
                      // testedMixer is not available due to security restrictions
                      continue;
                  } catch (Throwable thrown) {
                      out.println("## Mixer.open() threw unexpected exception:");
                      out.println("# Mixer = " + testedMixer);
                      thrown.printStackTrace(out);
                      testResult = STATUS_FAILED;
                      continue;
                  }
                  Line.Info supportedSourceLineInfo[] = null;
                  try {
                      supportedSourceLineInfo = testedMixer.getSourceLineInfo();
                  } catch (Throwable thrown) {
                      out.println("## Mixer.getSourceLineInfo() threw unexpected exception:");
                      out.println("# Mixer = " + testedMixer);
                      thrown.printStackTrace(out);
                      testResult = STATUS_FAILED;
                      testedMixer.close();
                      continue;
                  }
                  if ( supportedSourceLineInfo == null ) {
                      out.println("## Mixer.getSourceLineInfo() returned null array");
                      out.println("# Mixer = " + testedMixer);
                      testResult = STATUS_FAILED;
                      testedMixer.close();
                      continue;
                  }
                  out.println("\n>>> testedMixer["+i+"] = " + testedMixer);
                  out.println("\n>> supportedSourceLineInfo.length = " + supportedSourceLineInfo.length);

                  for (int j=0; j < supportedSourceLineInfo.length; j++) {
                      Line.Info testSourceLineInfo = supportedSourceLineInfo[j];
                      
                      Line testSourceLine = null;
                      try {
                          testSourceLine = testedMixer.getLine(testSourceLineInfo);
                      } catch (LineUnavailableException lineUnavailableException) {
                          // line is not available due to resource restrictions
                          continue;
                      } catch (SecurityException securityException) {
                          // line is not available due to security restrictions
                          continue;
                      } catch (Throwable thrown) {
                          out.println("## Mixer.getLine(Line.Info) threw unexpected Exception:");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# Line.Info = " + testSourceLineInfo);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                          continue;
                      }

                      out.println("\n> testSourceLineInfo["+j+"] = " + testSourceLineInfo);
                      out.println("> testSourceLine = " + testSourceLine);
                      if ( ! (testSourceLine instanceof SourceDataLine) ) {
                          out.println("> testSourceLine is not SourceDataLine");
                          continue;
                      }

                      SourceDataLine testedSourceLine = (SourceDataLine)testSourceLine;
                      AudioFormat lineAudioFormat = testedSourceLine.getFormat();

                      int bufferSizeToWrite = 1;
                      if ( lineAudioFormat.getSampleSizeInBits() != AudioSystem.NOT_SPECIFIED ) {
                          bufferSizeToWrite = lineAudioFormat.getSampleSizeInBits()/8;
                          if ( lineAudioFormat.getSampleSizeInBits()%8 != 0 ) {
                              bufferSizeToWrite++;
                          }
                      }
                      if ( lineAudioFormat.getFrameSize() != AudioSystem.NOT_SPECIFIED ) {
                          bufferSizeToWrite = lineAudioFormat.getFrameSize();
                      }
                      byte[] dataToWrite = new byte[bufferSizeToWrite];
                      for (int k=0; k < bufferSizeToWrite; k++) {
                          dataToWrite[k] = (byte)1;
                      }
                      int offsetToWrite = 0;

                      out.println
                          ("\n> check SourceDataLine.write() for not open line with correct length of data:");
                      int writtenBytes = -1;
                      try {
                          writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
                          out.println("> Bytes written: number of written bytes = " + writtenBytes);
                      } catch (Throwable thrown) {
                          out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
                          out.println("# Unexpected Exception is thrown");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# SourceDataLine = " + testedSourceLine);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                      }

                      out.println
                          ("\n> check SourceDataLine.write() for not open line with incorrect length of data:");
                      writtenBytes = -1;
                      bufferSizeToWrite--;
                      try {
                          writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
                          out.println("> Bytes written: number of written bytes = " + writtenBytes);
                      } catch (IllegalArgumentException illegalArgumentException) {
                          out.println("> Permissible IllegalArgumentException for the present instance is thrown:");
                          illegalArgumentException.printStackTrace(out);
                      } catch (Throwable thrown) {
                          out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
                          out.println("# Unexpected Exception is thrown");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# SourceDataLine = " + testedSourceLine);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                      }

                      out.println
                          ("\n> open tested line:");
                      bufferSizeToWrite++;
                      try {
                          testedSourceLine.open(lineAudioFormat, bufferSizeToWrite);
                          out.println("> OK - line is opened");
                      } catch (LineUnavailableException lineUnavailableException) {
                          out.println("> Line is not available due to resource restrictions:");
                          lineUnavailableException.printStackTrace(out);
                          continue;
                      } catch (SecurityException securityException) {
                          out.println("> Line is not available due to security restrictions:");
                          securityException.printStackTrace(out);
                          continue;
                      } catch (Throwable thrown) {
                          out.println("## SourceDataLine.open(AudioFormat format) failed:");
                          out.println("# Unexpected Exception is thrown");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# SourceDataLine = " + testedSourceLine);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                          continue;
                      }

                      out.println
                          ("\n> check SourceDataLine.write() for not started line with correct length of data:");
                      writtenBytes = -1;
                      try {
                          writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
                          out.println("> Bytes written: number of written bytes = " + writtenBytes);
                      } catch (Throwable thrown) {
                          out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
                          out.println("# Unexpected Exception is thrown");
                          out.println("# Mixer = " + testedMixer);
                          out.println("# SourceDataLine = " + testedSourceLine);
                          thrown.printStackTrace(out);
                          testResult = STATUS_FAILED;
                      }

                      out.println
                          ("\n> check SourceDataLine.write()

        Attachments

          Activity

            People

            • Assignee:
              fbomerssunw Florian Bomers (Inactive)
              Reporter:
              bondsunw Bond Bond (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: