src/jdk.internal.vm.compiler/share/classes/org.graalvm.graphio/src/org/graalvm/graphio/GraphProtocol.java
changeset 55509 d58442b8abc1
parent 54724 62f373a53296
child 58299 6df94ce3ab2f
equal deleted inserted replaced
55508:a6e2d06391d6 55509:d58442b8abc1
    26 
    26 
    27 import java.io.Closeable;
    27 import java.io.Closeable;
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.net.URI;
    29 import java.net.URI;
    30 import java.net.URISyntaxException;
    30 import java.net.URISyntaxException;
       
    31 import java.nio.Buffer;
    31 import java.nio.ByteBuffer;
    32 import java.nio.ByteBuffer;
    32 import java.nio.channels.WritableByteChannel;
    33 import java.nio.channels.WritableByteChannel;
    33 import java.nio.charset.Charset;
    34 import java.nio.charset.Charset;
    34 import java.util.Collection;
    35 import java.util.Collection;
    35 import java.util.HashMap;
    36 import java.util.HashMap;
    83     private final WritableByteChannel channel;
    84     private final WritableByteChannel channel;
    84     private final boolean embedded;
    85     private final boolean embedded;
    85     final int versionMajor;
    86     final int versionMajor;
    86     final int versionMinor;
    87     final int versionMinor;
    87     private boolean printing;
    88     private boolean printing;
       
    89 
       
    90     /**
       
    91      * See {@code org.graalvm.compiler.serviceprovider.BufferUtil}.
       
    92      */
       
    93     private static Buffer asBaseBuffer(Buffer obj) {
       
    94         return obj;
       
    95     }
    88 
    96 
    89     GraphProtocol(WritableByteChannel channel, int major, int minor, boolean embedded) throws IOException {
    97     GraphProtocol(WritableByteChannel channel, int major, int minor, boolean embedded) throws IOException {
    90         if (major > MAJOR_VERSION || (major == MAJOR_VERSION && minor > MINOR_VERSION)) {
    98         if (major > MAJOR_VERSION || (major == MAJOR_VERSION && minor > MINOR_VERSION)) {
    91             throw new IllegalArgumentException("Unrecognized version " + major + "." + minor);
    99             throw new IllegalArgumentException("Unrecognized version " + major + "." + minor);
    92         }
   100         }
   326             constantPool.reset();
   334             constantPool.reset();
   327         }
   335         }
   328     }
   336     }
   329 
   337 
   330     private void flush() throws IOException {
   338     private void flush() throws IOException {
   331         buffer.flip();
   339         asBaseBuffer(buffer).flip();
   332         /*
   340         /*
   333          * Try not to let interrupted threads abort the write. There's still a race here but an
   341          * Try not to let interrupted threads abort the write. There's still a race here but an
   334          * interrupt that's been pending for a long time shouldn't stop this writing.
   342          * interrupt that's been pending for a long time shouldn't stop this writing.
   335          */
   343          */
   336         boolean interrupted = Thread.interrupted();
   344         boolean interrupted = Thread.interrupted();
   409         int limit = b.limit();
   417         int limit = b.limit();
   410         int written = 0;
   418         int written = 0;
   411         while (b.position() < limit) {
   419         while (b.position() < limit) {
   412             int toWrite = Math.min(limit - b.position(), buffer.capacity());
   420             int toWrite = Math.min(limit - b.position(), buffer.capacity());
   413             ensureAvailable(toWrite);
   421             ensureAvailable(toWrite);
   414             b.limit(b.position() + toWrite);
   422             asBaseBuffer(b).limit(b.position() + toWrite);
   415             try {
   423             try {
   416                 buffer.put(b);
   424                 buffer.put(b);
   417                 written += toWrite;
   425                 written += toWrite;
   418             } finally {
   426             } finally {
   419                 b.limit(limit);
   427                 asBaseBuffer(b).limit(limit);
   420             }
   428             }
   421         }
   429         }
   422         return written;
   430         return written;
   423     }
   431     }
   424 
   432 
   428         } else {
   436         } else {
   429             writeInt(b.length);
   437             writeInt(b.length);
   430             int sizeInBytes = b.length * 4;
   438             int sizeInBytes = b.length * 4;
   431             ensureAvailable(sizeInBytes);
   439             ensureAvailable(sizeInBytes);
   432             buffer.asIntBuffer().put(b);
   440             buffer.asIntBuffer().put(b);
   433             buffer.position(buffer.position() + sizeInBytes);
   441             asBaseBuffer(buffer).position(buffer.position() + sizeInBytes);
   434         }
   442         }
   435     }
   443     }
   436 
   444 
   437     private void writeDoubles(double[] b) throws IOException {
   445     private void writeDoubles(double[] b) throws IOException {
   438         if (b == null) {
   446         if (b == null) {
   440         } else {
   448         } else {
   441             writeInt(b.length);
   449             writeInt(b.length);
   442             int sizeInBytes = b.length * 8;
   450             int sizeInBytes = b.length * 8;
   443             ensureAvailable(sizeInBytes);
   451             ensureAvailable(sizeInBytes);
   444             buffer.asDoubleBuffer().put(b);
   452             buffer.asDoubleBuffer().put(b);
   445             buffer.position(buffer.position() + sizeInBytes);
   453             asBaseBuffer(buffer).position(buffer.position() + sizeInBytes);
   446         }
   454         }
   447     }
   455     }
   448 
   456 
   449     private void writePoolObject(Object obj) throws IOException {
   457     private void writePoolObject(Object obj) throws IOException {
   450         Object object = obj;
   458         Object object = obj;