Java Code Examples for org.osgl.util.IO#write()

The following examples show how to use org.osgl.util.IO#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IdGenerator.java    From actframework with Apache License 2.0 5 votes vote down vote up
public FileBasedStartCounter(String path) {
    File file = new File(path);
    if (file.exists()) {
        String s = IO.readContentAsString(file);
        long seq = Long.parseLong(s);
        seq = seq + 1;
        IO.write(S.str(seq), file);
        id = (seq);
    } else {
        id = 0;
        IO.write(Long.toString(id), file);
    }
}
 
Example 2
Source File: MailerEnhancerTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized Class<?> loadClass(final String name,
                                          final boolean resolve) throws ClassNotFoundException {
    if (!name.startsWith("testapp.")) {
        return super.loadClass(name, resolve);
    }

    // gets an input stream to read the bytecode of the class
    String cn = name.replace('.', '/');
    String resource = cn + ".class";
    InputStream is = getResourceAsStream(resource);
    byte[] b;

    // adapts the class on the fly
    try {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(0);
        MailerEnhancer enhancer = new MailerEnhancer(cw, MailerEnhancerTest.this);
        cr.accept(enhancer, 0);
        b = cw.toByteArray();
        OutputStream os1 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".class");
        IO.write(b, os1);
        cr = new ClassReader(b);
        cw = new ClassWriter(0);
        OutputStream os2 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".java");
        ClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(os2));
        cr.accept(tv, 0);
    } catch (Exception e) {
        throw new ClassNotFoundException(name, e);
    }

    // returns the adapted class
    return defineClass(name, b, 0, b.length);
}
 
Example 3
Source File: ControllerEnhancerTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized Class<?> loadClass(final String name,
                                          final boolean resolve) throws ClassNotFoundException {
    if (!name.startsWith("testapp.")) {
        return super.loadClass(name, resolve);
    }

    // gets an input stream to read the bytecode of the class
    String cn = name.replace('.', '/');
    String resource = cn + ".class";
    InputStream is = getResourceAsStream(resource);
    byte[] b;

    // adapts the class on the fly
    try {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(0);
        ControllerEnhancer enhancer = new ControllerEnhancer(cw, ControllerEnhancerTest.this);
        cr.accept(enhancer, 0);
        b = cw.toByteArray();
        OutputStream os1 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".class");
        IO.write(b, os1);
        cr = new ClassReader(b);
        cw = new ClassWriter(0);
        OutputStream os2 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".java");
        ClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(os2));
        cr.accept(tv, 0);
    } catch (Exception e) {
        throw new ClassNotFoundException(name, e);
    }

    // returns the adapted class
    return defineClass(name, b, 0, b.length);
}
 
Example 4
Source File: MetricEnhancerTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized Class<?> loadClass(final String name,
                                          final boolean resolve) throws ClassNotFoundException {
    if (!name.equals("testapp.metric.TestBed")) {
        return super.loadClass(name, resolve);
    }

    // gets an input stream to read the bytecode of the class
    String cn = name.replace('.', '/');
    String resource = cn + ".class";
    InputStream is = getResourceAsStream(resource);
    byte[] b;

    // adapts the class on the fly
    try {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        MetricEnhancer enhancer = new MetricEnhancer(repo, cw);
        cr.accept(enhancer, ClassReader.EXPAND_FRAMES);
        b = cw.toByteArray();
        OutputStream os1 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".class");
        IO.write(b, os1);
        cr = new ClassReader(b);
        cw = new ClassWriter(0);
        OutputStream os2 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".java");
        ClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(os2));
        cr.accept(tv, 0);
    } catch (Exception e) {
        throw new ClassNotFoundException(name, e);
    }

    // returns the adapted class
    return defineClass(name, b, 0, b.length);
}
 
Example 5
Source File: DataObjectEnhancerTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized Class<?> loadClass(final String name,
                                          final boolean resolve) throws ClassNotFoundException {
    if (!name.startsWith("testapp.")) {
        return super.loadClass(name, resolve);
    }

    // gets an input stream to read the bytecode of the class
    String cn = name.replace('.', '/');
    String resource = cn + ".class";
    InputStream is = getResourceAsStream(resource);
    byte[] b;

    // adapts the class on the fly
    try {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        DataObjectEnhancer enhancer = new DataObjectEnhancer(cw);
        cr.accept(enhancer, 0);
        b = cw.toByteArray();
        //CheckClassAdapter.verify(new ClassReader(cw.toByteArray()), true, new PrintWriter(System.out));
        OutputStream os1 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".class");
        IO.write(b, os1);
        cr = new ClassReader(b);
        cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        OutputStream os2 = new FileOutputStream("/tmp/" + S.afterLast(cn, "/") + ".java");
        ClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(os2));
        cr.accept(tv, 0);
    } catch (Exception e) {
        throw new ClassNotFoundException(name, e);
    }

    // returns the adapted class
    return defineClass(name, b, 0, b.length);
}