Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Writing to a DVD/CD - Java [closed]

I'm working on a application called LMCT(Let Me Copy That) and it is programed in Java, I just need to know how to burn to a DVD/CD from java.

any example, API or link is welcome.

like image 375
Liam Haworth Avatar asked Dec 19 '11 01:12

Liam Haworth


1 Answers

I've done this using COM4J and IMAPI (works on Windows only).

This sample code burns an ISO file using the first CD recording device in the system:

File isoFile = new File("myimage.iso");

IDiscMaster2 dm = ClassFactory.createMsftDiscMaster2();
int count = dm.count();

//Pick the first recorder on the system
String recorderUniqueId = null;
for (int i = 0; i < count; i++)
{
    String cur = dm.item(i);
    recorderUniqueId = cur;
}

IDiscRecorder2 recorder = ClassFactory.createMsftDiscRecorder2();
recorder.initializeDiscRecorder(recorderUniqueId);
System.out.println("Using recorder: " + recorder.vendorId() + " " + recorder.productId());

IIsoImageManager imageManager = StreamClassFactory.createMsftIsoImageManager();
imageManager.setPath(isoFile.getAbsolutePath());
imageManager.validate();
System.out.println("ISO Validation successful: " + isoFile.getAbsolutePath());

IDiscFormat2DataExt discData = ClassFactoryExt.createMsftDiscFormat2Data();
discData.recorder(recorder);
discData.clientName(IsoMan.class.getSimpleName());
int mediaStatus = discData.currentMediaStatusExt();
System.out.println("Media status: " + mediaStatus);
if ((mediaStatus & IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED.comEnumValue()) != 0)
    throw new RuntimeException("Media is write protected / not empty.");

//Check if disc is empty
int addr = discData.nextWritableAddress();
if (addr != 0)
    throw new RuntimeException("Disc is not empty, not writing.");

IStream isoStream = imageManager.stream();

System.out.println("Writing CD");
discData.write(isoStream);
System.out.println("Finished writing");

I had to supplement the COM4J generated classes a bit because the generator didn't do a perfect job:

/**
 * Defines methods to create COM objects
 */
public abstract class ClassFactoryExt {
    private ClassFactoryExt() {} // instanciation is not allowed

    /**
     * Microsoft IMAPIv2 Data Writer
     */
    public static com.ms.imapi2.IDiscFormat2DataExt createMsftDiscFormat2Data() {
        return COM4J.createInstance( com.ms.imapi2.IDiscFormat2DataExt.class, "{2735412A-7F64-5B0F-8F00-5D77AFBE261E}" );
    }
}

/**
 * Data Writer
 */
@IID("{27354153-9F64-5B0F-8F00-5D77AFBE261E}")
public interface IDiscFormat2DataExt extends com.ms.imapi2.IDiscFormat2Data {
    /**
     * The state (usability) of the current media
     */
    @VTID(18)
    //com.ms.imapi2.IMAPI_FORMAT2_DATA_MEDIA_STATE currentMediaStatus();
    int currentMediaStatusExt();
}

/**
 * Defines methods to create COM objects
 */
public abstract class StreamClassFactory {
    private StreamClassFactory() {} // instanciation is not allowed


    /**
     * Boot options
     */
    public static com.ms.imapi2.IBootOptions createBootOptions() {
        return COM4J.createInstance( com.ms.imapi2.IBootOptions.class, "{2C941FCE-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * File system image
     */
    public static com.ms.imapi2.IFileSystemImage3 createMsftFileSystemImage() {
        return COM4J.createInstance( com.ms.imapi2.IFileSystemImage3.class, "{2C941FC5-975B-59BE-A960-9A2A262853A5}" );
    }

    /**
     * Microsoft IMAPIv2 Iso Image Manager
     */
    public static com.ms.imapi2.IIsoImageManager createMsftIsoImageManager() {
        return COM4J.createInstance( com.ms.imapi2.IIsoImageManager.class, "{CEEE3B62-8F56-4056-869B-EF16917E3EFC}" );
    }
}
like image 57
prunge Avatar answered Nov 16 '22 03:11

prunge